-- db/queries.sql
-- name: GetConfig :one
SELECT * FROM config LIMIT 1;

-- name: UpsertConfig :exec
INSERT INTO config (api_token, zone_id, domain, update_period)
VALUES (?, ?, ?, ?)
ON CONFLICT DO UPDATE SET
    api_token = excluded.api_token,
    zone_id = excluded.zone_id,
    domain = excluded.domain,
    update_period = excluded.update_period;

-- name: DeleteAllConfig :exec
DELETE FROM config;

-- name: InsertConfig :exec
INSERT INTO config (api_token, zone_id, domain, update_period)
VALUES (?, ?, ?, ?);

-- name: InitSchema :exec
CREATE TABLE IF NOT EXISTS config (
    api_token TEXT NOT NULL DEFAULT '',
    zone_id TEXT NOT NULL DEFAULT '',
    domain TEXT NOT NULL DEFAULT 'mz.uy',
    update_period TEXT NOT NULL DEFAULT '0 */6 * * *'
);

CREATE TABLE IF NOT EXISTS static_records (
    record_id TEXT PRIMARY KEY,
    record_name TEXT NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Insert default config if none exists
INSERT OR IGNORE INTO config (api_token, zone_id, domain, update_period)
SELECT '', '', 'mz.uy', '0 */6 * * *'
WHERE NOT EXISTS (SELECT 1 FROM config);

-- name: AddStaticRecord :exec
INSERT OR REPLACE INTO static_records (record_id, record_name)
VALUES (?, ?);

-- name: RemoveStaticRecord :exec
DELETE FROM static_records WHERE record_id = ?;

-- name: GetStaticRecords :many
SELECT record_id, record_name FROM static_records;

-- name: IsStaticRecord :one
SELECT CAST(EXISTS(SELECT 1 FROM static_records WHERE record_id = ?) AS BOOLEAN) AS is_static;
