package store

import (
	"database/sql"
	"errors"
	"fmt"

	"github.com/google/uuid"
)

// CreateRegistry inserts a new registry.
func (s *Store) CreateRegistry(r Registry) (Registry, error) {
	r.ID = uuid.New().String()
	r.CreatedAt = Now()
	r.UpdatedAt = r.CreatedAt

	_, err := s.db.Exec(
		`INSERT INTO registries (id, name, url, type, token, owner, created_at, updated_at)
		 VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
		r.ID, r.Name, r.URL, r.Type, r.Token, r.Owner, r.CreatedAt, r.UpdatedAt,
	)
	if err != nil {
		return Registry{}, fmt.Errorf("insert registry: %w", err)
	}
	return r, nil
}

// GetRegistryByID returns a single registry by its ID.
func (s *Store) GetRegistryByID(id string) (Registry, error) {
	var r Registry
	err := s.db.QueryRow(
		`SELECT id, name, url, type, token, owner, created_at, updated_at
		 FROM registries WHERE id = ?`, id,
	).Scan(&r.ID, &r.Name, &r.URL, &r.Type, &r.Token, &r.Owner, &r.CreatedAt, &r.UpdatedAt)
	if errors.Is(err, sql.ErrNoRows) {
		return Registry{}, fmt.Errorf("registry %s: %w", id, ErrNotFound)
	}
	if err != nil {
		return Registry{}, fmt.Errorf("query registry: %w", err)
	}
	return r, nil
}

// GetRegistryByName returns a single registry by its unique name.
func (s *Store) GetRegistryByName(name string) (Registry, error) {
	var r Registry
	err := s.db.QueryRow(
		`SELECT id, name, url, type, token, owner, created_at, updated_at
		 FROM registries WHERE name = ?`, name,
	).Scan(&r.ID, &r.Name, &r.URL, &r.Type, &r.Token, &r.Owner, &r.CreatedAt, &r.UpdatedAt)
	if errors.Is(err, sql.ErrNoRows) {
		return Registry{}, fmt.Errorf("registry %q: %w", name, ErrNotFound)
	}
	if err != nil {
		return Registry{}, fmt.Errorf("query registry by name: %w", err)
	}
	return r, nil
}

// GetAllRegistries returns every registry ordered by name.
func (s *Store) GetAllRegistries() ([]Registry, error) {
	rows, err := s.db.Query(
		`SELECT id, name, url, type, token, owner, created_at, updated_at
		 FROM registries ORDER BY name`,
	)
	if err != nil {
		return nil, fmt.Errorf("query registries: %w", err)
	}
	defer rows.Close()

	registries := []Registry{}
	for rows.Next() {
		var r Registry
		if err := rows.Scan(&r.ID, &r.Name, &r.URL, &r.Type, &r.Token, &r.Owner, &r.CreatedAt, &r.UpdatedAt); err != nil {
			return nil, fmt.Errorf("scan registry: %w", err)
		}
		registries = append(registries, r)
	}
	return registries, rows.Err()
}

// UpdateRegistry updates an existing registry's mutable fields.
func (s *Store) UpdateRegistry(r Registry) error {
	r.UpdatedAt = Now()
	result, err := s.db.Exec(
		`UPDATE registries SET name=?, url=?, type=?, token=?, owner=?, updated_at=?
		 WHERE id=?`,
		r.Name, r.URL, r.Type, r.Token, r.Owner, r.UpdatedAt, r.ID,
	)
	if err != nil {
		return fmt.Errorf("update registry: %w", err)
	}
	n, _ := result.RowsAffected()
	if n == 0 {
		return fmt.Errorf("registry %s: %w", r.ID, ErrNotFound)
	}
	return nil
}

// DeleteRegistry removes a registry by ID.
func (s *Store) DeleteRegistry(id string) error {
	result, err := s.db.Exec(`DELETE FROM registries WHERE id = ?`, id)
	if err != nil {
		return fmt.Errorf("delete registry: %w", err)
	}
	n, _ := result.RowsAffected()
	if n == 0 {
		return fmt.Errorf("registry %s: %w", id, ErrNotFound)
	}
	return nil
}
