/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package maindb

import (
	"context"

	"mstore/pkg/descr"
)

func (db *Database) InsertBlob(ctx context.Context, layer *descr.Blob) error {
	var err error
	request := `
                INSERT INTO blobs(id, name, reference, mediaType, digest, size,
                        created_at, updated_at, created_by, updated_by)
                        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
	_, err = db.db.Exec(request,
		layer.ID, layer.Name, layer.Reference, layer.MediaType, layer.Digest, layer.Size,
		layer.CreatedAt, layer.UpdatedAt,
		layer.CreatedBy, layer.UpdatedBy)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) xxxGetBlobByDigest(ctx context.Context, digest string) (bool, descr.Blob, error) {
	var err error
	blobs := make([]descr.Blob, 0)
	res := descr.Blob{}
	exists := false
	request := `SELECT * FROM blobs WHERE digest = $1 LIMIT 1`
	err = db.db.Select(&blobs, request, digest)
	if err != nil {
		return exists, res, err
	}
	if len(blobs) > 0 {
		res = blobs[0]
		exists = true
	}
	return exists, res, err
}

func (db *Database) GetBlobByNameDigest(ctx context.Context, name, digest string) (bool, descr.Blob, error) {
	var err error
	blobs := make([]descr.Blob, 0)
	res := descr.Blob{}
	exists := false
	request := `SELECT * FROM blobs WHERE name = $1 AND digest = $2 LIMIT 1`
	err = db.db.Select(&blobs, request, name, digest)
	if err != nil {
		return exists, res, err
	}
	if len(blobs) > 0 {
		res = blobs[0]
		exists = true
	}
	return exists, res, err
}

func (db *Database) GetBlobByNameRefDigest(ctx context.Context, name, reference, digest string) (bool, descr.Blob, error) {
	var err error
	blobs := make([]descr.Blob, 0)
	res := descr.Blob{}
	exists := false
	request := `SELECT * FROM blobs WHERE name = $1 AND reference = $2 AND digest = $3 LIMIT 1`
	err = db.db.Select(&blobs, request, name, reference, digest)
	if err != nil {
		return exists, res, err
	}
	if len(blobs) > 0 {
		res = blobs[0]
		exists = true
	}
	return exists, res, err
}

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

func (db *Database) xxxBlobExists(ctx context.Context, name, reference, digest string) (bool, error) {
	var err error
	blobs := make([]descr.Blob, 0)
	request := `
                SELECT * FROM blobs WHERE name = $1 AND reference = $2 AND digest = $3 LIMIT 1`
	err = db.db.Select(&blobs, request, name, reference, digest)
	if err != nil {
		return false, err
	}
	if len(blobs) > 0 {
		return true, err
	}
	return false, err
}

func (db *Database) GetBlobsByReferense(ctx context.Context, name, reference string) ([]descr.Blob, error) {
	var err error
	blobs := make([]descr.Blob, 0)
	request := `
                SELECT * FROM blobs WHERE name = $1 AND reference = $2`
	err = db.db.Select(&blobs, request, name, reference)
	if err != nil {
		return blobs, err
	}
	return blobs, err
}

func (db *Database) GetBlobUsage(ctx context.Context, name, digest string) (int64, error) {
	var err error
	var usage int64
	count := make([]int64, 0)
	request := `SELECT count(id) AS count FROM blobs WHERE name = $1 AND digest = $2`
	err = db.db.Select(&count, request, name, digest)
	if err != nil {
		return usage, err
	}
	usage = count[0]
	return usage, err
}

func (db *Database) xxxxDeleteBlobByDigest(ctx context.Context, digest string) error {
	var err error
	request := `DELETE FROM blobs WHERE digest = $1;`
	_, err = db.db.Exec(request, digest)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) DeleteBlobByNameDigest(ctx context.Context, name, digest string) error {
	var err error
	request := `DELETE FROM blobs WHERE name = $1 AND digest = $2;`
	_, err = db.db.Exec(request, name, digest)
	if err != nil {
		return err
	}
	return err
}
