package sqlite

import (
	"context"
	"database/sql"

	"switchboard-core/models"
)

// ── DependencyStore (v0.38.2) ────────────────────────

type DependencyStore struct{}

func NewDependencyStore() *DependencyStore { return &DependencyStore{} }

const depCols = `consumer_id, library_id, version_spec, resolved_ver`

func (s *DependencyStore) Create(ctx context.Context, dep *models.ExtDependency) error {
	_, err := DB.ExecContext(ctx, `
		INSERT OR REPLACE INTO ext_dependencies (consumer_id, library_id, version_spec, resolved_ver)
		VALUES (?, ?, ?, ?)`,
		dep.ConsumerID, dep.LibraryID, dep.VersionSpec, dep.ResolvedVer,
	)
	return err
}

func (s *DependencyStore) Delete(ctx context.Context, consumerID, libraryID string) error {
	_, err := DB.ExecContext(ctx,
		`DELETE FROM ext_dependencies WHERE consumer_id = ? AND library_id = ?`,
		consumerID, libraryID)
	return err
}

func (s *DependencyStore) DeleteAllForConsumer(ctx context.Context, consumerID string) error {
	_, err := DB.ExecContext(ctx,
		`DELETE FROM ext_dependencies WHERE consumer_id = ?`, consumerID)
	return err
}

func (s *DependencyStore) ListByConsumer(ctx context.Context, consumerID string) ([]models.ExtDependency, error) {
	rows, err := DB.QueryContext(ctx,
		`SELECT `+depCols+` FROM ext_dependencies WHERE consumer_id = ? ORDER BY library_id`,
		consumerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	return scanDependencies(rows)
}

func (s *DependencyStore) ListByLibrary(ctx context.Context, libraryID string) ([]models.ExtDependency, error) {
	rows, err := DB.QueryContext(ctx,
		`SELECT `+depCols+` FROM ext_dependencies WHERE library_id = ? ORDER BY consumer_id`,
		libraryID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	return scanDependencies(rows)
}

func (s *DependencyStore) ListAll(ctx context.Context) ([]models.ExtDependency, error) {
	rows, err := DB.QueryContext(ctx,
		`SELECT `+depCols+` FROM ext_dependencies ORDER BY consumer_id, library_id`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	return scanDependencies(rows)
}

func (s *DependencyStore) HasConsumers(ctx context.Context, libraryID string) (bool, error) {
	var count int
	err := DB.QueryRowContext(ctx,
		`SELECT COUNT(*) FROM ext_dependencies WHERE library_id = ?`,
		libraryID).Scan(&count)
	return count > 0, err
}

// ── Internal ───────────────────────────────────────

func scanDependencies(rows *sql.Rows) ([]models.ExtDependency, error) {
	var result []models.ExtDependency
	for rows.Next() {
		var d models.ExtDependency
		err := rows.Scan(&d.ConsumerID, &d.LibraryID, &d.VersionSpec, &d.ResolvedVer)
		if err != nil {
			return nil, err
		}
		result = append(result, d)
	}
	return result, rows.Err()
}
