// Copyright 2023 Igor Zibarev
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sql

import (
	"context"
	"fmt"

	"github.com/uptrace/bun"

	"github.com/auditumio/auditum/internal/aud"
)

func partitionForProjectTableName(table string, ppn int32) string {
	// ppn stands for "project partition number".
	return fmt.Sprintf("%s_ppn_%d", table, ppn)
}

func createTablePartitionForProject(
	ctx context.Context,
	idb bun.IDB,
	ofTableName string,
	projectID aud.ID,
	partitionNumber int32,
) error {
	q := fmt.Sprintf(
		`CREATE TABLE %s PARTITION OF %s FOR VALUES IN (?);`,
		partitionForProjectTableName(ofTableName, partitionNumber),
		ofTableName,
	)

	_, err := idb.ExecContext(ctx, q, projectID)
	if err != nil {
		return fmt.Errorf("create table in db: %v", err)
	}

	return nil
}

func dropTablePartitionForProject(
	ctx context.Context,
	idb bun.IDB,
	ofTableName string,
	partitionNumber int32,
) error {
	_, err := idb.NewDropTable().
		TableExpr(partitionForProjectTableName(ofTableName, partitionNumber)).
		Cascade().
		Exec(ctx)
	if err != nil {
		return fmt.Errorf("drop table in db: %v", err)
	}

	return nil
}
