package database

import (
	"Portifolio/internal/model"
	"database/sql"
	"fmt"

	_ "github.com/mattn/go-sqlite3"
)

func GetCurrencyByID(db *sql.DB, ID int) (model.Currency, error) {
	var c model.Currency
	err := db.QueryRow(
		`SELECT id, code, name, FROM currencies WHERE id = ?`,
		ID,
	).Scan(&c.ID, &c.Code, &c.Name)
	if err == sql.ErrNoRows {
		return c, fmt.Errorf("company %d not found", ID)
	}
	return c, nil
}

/*
   CREATE TABLE IF NOT EXISTS currencies (
       id   INTEGER PRIMARY KEY AUTOINCREMENT,
       code TEXT    NOT NULL UNIQUE,
       name TEXT    NOT NULL
   );
*/

func GetCurrencyByCode(db *sql.DB, Code string) (model.Currency, error) {
	var c model.Currency
	err := db.QueryRow(
		`SELECT id, code, name FROM currencies WHERE code = ?`,
		Code,
	).Scan(&c.ID, &c.Code, &c.Name)
	if err == sql.ErrNoRows {
		return c, fmt.Errorf("company %s not found", Code)
	}
	return c, nil
}
