package store

// import (
// 	"context"
// 	"encoding/json"
// 	"fmt"
// 	"strings"

// 	"github.com/google/uuid"

// 	"git.warky.dev/wdevs/amcs/internal/generatedmodels"
// 	ext "git.warky.dev/wdevs/amcs/internal/types"
// )

// func (db *DB) AddHouseholdItem(ctx context.Context, item ext.HouseholdItem) (ext.HouseholdItem, error) {
// 	details, err := json.Marshal(item.Details)
// 	if err != nil {
// 		return ext.HouseholdItem{}, fmt.Errorf("marshal details: %w", err)
// 	}

// 	row := db.pool.QueryRow(ctx, `
// 		insert into household_items (name, category, location, details, notes)
// 		values ($1, $2, $3, $4::jsonb, $5)
// 		returning id, created_at, updated_at
// 	`, item.Name, nullStr(item.Category), nullStr(item.Location), details, nullStr(item.Notes))

// 	created := item
// 	var model generatedmodels.ModelPublicHouseholdItems
// 	if err := row.Scan(&model.ID, &model.CreatedAt, &model.UpdatedAt); err != nil {
// 		return ext.HouseholdItem{}, fmt.Errorf("insert household item: %w", err)
// 	}
// 	created.ID = model.ID.UUID()
// 	created.CreatedAt = model.CreatedAt.Time()
// 	created.UpdatedAt = model.UpdatedAt.Time()
// 	return created, nil
// }

// func (db *DB) SearchHouseholdItems(ctx context.Context, query, category, location string) ([]ext.HouseholdItem, error) {
// 	args := []any{}
// 	conditions := []string{}

// 	if q := strings.TrimSpace(query); q != "" {
// 		args = append(args, "%"+q+"%")
// 		conditions = append(conditions, fmt.Sprintf("(name ILIKE $%d OR notes ILIKE $%d)", len(args), len(args)))
// 	}
// 	if c := strings.TrimSpace(category); c != "" {
// 		args = append(args, c)
// 		conditions = append(conditions, fmt.Sprintf("category = $%d", len(args)))
// 	}
// 	if l := strings.TrimSpace(location); l != "" {
// 		args = append(args, "%"+l+"%")
// 		conditions = append(conditions, fmt.Sprintf("location ILIKE $%d", len(args)))
// 	}

// 	q := `select id, name, category, location, details, notes, created_at, updated_at from household_items`
// 	if len(conditions) > 0 {
// 		q += " where " + strings.Join(conditions, " and ")
// 	}
// 	q += " order by name"

// 	rows, err := db.pool.Query(ctx, q, args...)
// 	if err != nil {
// 		return nil, fmt.Errorf("search household items: %w", err)
// 	}
// 	defer rows.Close()

// 	var items []ext.HouseholdItem
// 	for rows.Next() {
// 		var model generatedmodels.ModelPublicHouseholdItems
// 		if err := rows.Scan(&model.ID, &model.Name, &model.Category, &model.Location, &model.Details, &model.Notes, &model.CreatedAt, &model.UpdatedAt); err != nil {
// 			return nil, fmt.Errorf("scan household item: %w", err)
// 		}
// 		items = append(items, householdItemFromModel(model))
// 	}
// 	return items, rows.Err()
// }

// func (db *DB) GetHouseholdItem(ctx context.Context, id uuid.UUID) (ext.HouseholdItem, error) {
// 	row := db.pool.QueryRow(ctx, `
// 		select id, name, category, location, details, notes, created_at, updated_at
// 		from household_items where id = $1
// 	`, id)

// 	var model generatedmodels.ModelPublicHouseholdItems
// 	if err := row.Scan(&model.ID, &model.Name, &model.Category, &model.Location, &model.Details, &model.Notes, &model.CreatedAt, &model.UpdatedAt); err != nil {
// 		return ext.HouseholdItem{}, fmt.Errorf("get household item: %w", err)
// 	}
// 	return householdItemFromModel(model), nil
// }

// func (db *DB) AddVendor(ctx context.Context, v ext.HouseholdVendor) (ext.HouseholdVendor, error) {
// 	row := db.pool.QueryRow(ctx, `
// 		insert into household_vendors (name, service_type, phone, email, website, notes, rating, last_used)
// 		values ($1, $2, $3, $4, $5, $6, $7, $8)
// 		returning id, created_at
// 	`, v.Name, nullStr(v.ServiceType), nullStr(v.Phone), nullStr(v.Email),
// 		nullStr(v.Website), nullStr(v.Notes), v.Rating, v.LastUsed)

// 	created := v
// 	var model generatedmodels.ModelPublicHouseholdVendors
// 	if err := row.Scan(&model.ID, &model.CreatedAt); err != nil {
// 		return ext.HouseholdVendor{}, fmt.Errorf("insert vendor: %w", err)
// 	}
// 	created.ID = model.ID.UUID()
// 	created.CreatedAt = model.CreatedAt.Time()
// 	return created, nil
// }

// func (db *DB) ListVendors(ctx context.Context, serviceType string) ([]ext.HouseholdVendor, error) {
// 	args := []any{}
// 	q := `select id, name, service_type, phone, email, website, notes, rating, last_used, created_at from household_vendors`
// 	if st := strings.TrimSpace(serviceType); st != "" {
// 		args = append(args, st)
// 		q += " where service_type = $1"
// 	}
// 	q += " order by name"

// 	rows, err := db.pool.Query(ctx, q, args...)
// 	if err != nil {
// 		return nil, fmt.Errorf("list vendors: %w", err)
// 	}
// 	defer rows.Close()

// 	var vendors []ext.HouseholdVendor
// 	for rows.Next() {
// 		var model generatedmodels.ModelPublicHouseholdVendors
// 		if err := rows.Scan(&model.ID, &model.Name, &model.ServiceType, &model.Phone, &model.Email, &model.Website, &model.Notes, &model.Rating, &model.LastUsed, &model.CreatedAt); err != nil {
// 			return nil, fmt.Errorf("scan vendor: %w", err)
// 		}
// 		vendors = append(vendors, householdVendorFromModel(model))
// 	}
// 	return vendors, rows.Err()
// }
