/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 *
 * This work is published and licensed under a Creative Commons
 * Attribution-NonCommercial-NoDerivatives 4.0 International License.
 *
 * Distribution of this work is permitted, but commercial use and
 * modifications are strictly prohibited.
 */
package maindb

import (
	"context"

	"mstore/pkg/descr"
	"mstore/pkg/term"
	"mstore/pkg/uuid"
)

func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
	var err error
	request := `INSERT INTO grants(id, account_id, right, pattern, created_at, updated_at, created_by, updated_by)
                        VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
	_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Right, grant.Pattern,
		grant.CreatedAt, grant.UpdatedAt, grant.CreatedBy, grant.UpdatedBy)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) UpdateGrantByID(ctx context.Context, grantID uuid.UUID, grant *descr.Grant) error {
	var err error
	request := `UPDATE grants SET pattern = $1, updated_at = $2, updated_by = $3 WHERE id = $4`
	_, err = db.db.Exec(request, grant.Pattern, grant.UpdatedAt, grant.UpdatedBy, grantID)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) ListGrantsByAccountID(ctx context.Context, accountID uuid.UUID) ([]descr.Grant, error) {
	var err error
	request := `SELECT * FROM grants WHERE account_id = $1`
	res := make([]descr.Grant, 0)
	err = db.db.Select(&res, request, accountID)
	if err != nil {
		return res, err
	}
	return res, err
}

func (db *Database) ListGrants(ctx context.Context) ([]descr.Grant, error) {
	var err error
	request := `SELECT * FROM grants`
	res := make([]descr.Grant, 0)
	err = db.db.Select(&res, request)
	if err != nil {
		return res, err
	}
	return res, err
}

func (db *Database) GetGrantByID(ctx context.Context, id uuid.UUID) (bool, *descr.Grant, error) {
	var err error
	res := &descr.Grant{}
	request := `SELECT * FROM grants WHERE id = $1 LIMIT 1`
	dbRes := make([]descr.Grant, 0)
	err = db.db.Select(&dbRes, request, id)
	if err != nil {
		return false, res, err
	}
	if len(dbRes) == 0 {
		return false, res, err

	}
	res = &dbRes[0]
	return true, res, err
}

func (db *Database) GetGrantByAccoundIDRight(ctx context.Context, accountID uuid.UUID, right term.Right) (bool, *descr.Grant, error) {
	var err error
	res := &descr.Grant{}
	request := `SELECT * FROM grants WHERE account_id = $1 AND right = $2 LIMIT 1`
	dbRes := make([]descr.Grant, 0)
	err = db.db.Select(&dbRes, request, accountID, right)
	if err != nil {
		return false, res, err
	}
	if len(dbRes) == 0 {
		return false, res, err

	}
	res = &dbRes[0]
	return true, res, err
}

func (db *Database) ListGrantsByAccoundIDRight(ctx context.Context, accountID uuid.UUID, right term.Right) (bool, []descr.Grant, error) {
	var err error
	request := `SELECT * FROM grants WHERE account_id = $1 AND right = $2`
	res := make([]descr.Grant, 0)
	err = db.db.Select(&res, request, accountID, right)
	if err != nil {
		return false, res, err
	}
	if len(res) == 0 {
		return false, res, err
	}
	return true, res, err
}

func (db *Database) GetGrantByAccoundIDRightPattern(ctx context.Context, accountID uuid.UUID, right term.Right, pattern string) (bool, *descr.Grant, error) {
	var err error
	res := &descr.Grant{}
	request := `SELECT * FROM grants WHERE account_id = $1 AND right = $2 AND pattern = $3 LIMIT 1`
	dbRes := make([]descr.Grant, 0)
	err = db.db.Select(&dbRes, request, accountID, right, pattern)
	if err != nil {
		return false, res, err
	}
	if len(dbRes) == 0 {
		return false, res, err

	}
	res = &dbRes[0]
	return true, res, err
}

func (db *Database) DeleteGrantByAccountIDRightPattern(ctx context.Context, accountID uuid.UUID, right, pattern string) error {
	var err error
	request := `DELETE FROM grants WHERE account_id = $1 AND right = $2 AND pattern = $3`
	_, err = db.db.Exec(request, accountID, right, pattern)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) DeleteGrantByID(ctx context.Context, grantID uuid.UUID) error {
	var err error
	request := `DELETE FROM grants WHERE id = $1`
	_, err = db.db.Exec(request, grantID)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) DeleteAllGrantsForAccountID(ctx context.Context, grantID uuid.UUID) error {
	var err error
	request := `DELETE FROM grants WHERE account_id = $1`
	_, err = db.db.Exec(request, grantID)
	if err != nil {
		return err
	}
	return err
}
