-- Seed dev: CRA local en esta máquina (instancia Alfa).
-- Aplica sobre a24c en Postgres. Idempotente donde es posible.
-- Contraseñas cifradas con ENCRYPTION_KEY del .env del panel (Soluciones01).

SET search_path TO a24c, public;

-- Bitácora: permitir status forwarded (reenvío SFTP).
ALTER TABLE a24c.restore_job_logs DROP CONSTRAINT IF EXISTS restore_job_logs_status_check;
ALTER TABLE a24c.restore_job_logs ADD CONSTRAINT restore_job_logs_status_check
    CHECK (status IN ('completed', 'failed', 'forwarded'));

-- Servidor Alfa: SQL local + SSH local (misma máquina Windows).
UPDATE a24c.restore_targets
SET
    server_ip               = 'localhost',
    sql_username            = 'sa',
    sql_password_encrypted  = 'gcm:hp9C+T5Zvdxqc/CS:S+56zEHeJFszt3MptzjS9A==:1UTe60xRrMSOW0/+',
    data_folder             = 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA',
    ssh_host                = 'localhost',
    ssh_port                = 22,
    ssh_username            = 'hugo_reyes',
    ssh_password_encrypted  = 'gcm:iJgYS7AJ1osrrwHj:8D4TiN9xOEr207+HBIxt8w==:/wt/4ZwPY4Xg4CLA',
    remote_inbox_path       = 'C:\CloudRestore\Entrada',
    notes                   = 'CRA dev local — esta máquina',
    updated_at              = now()
WHERE name = 'Alfa';

-- Carpeta de entrada reportada (hasta que el CRA guarde config real).
INSERT INTO a24c.cloudrestore_status (instance_key, input_folder, host_name, app_version, reported_at)
VALUES ('Alfa', 'C:\CloudRestore\Entrada', 'DEV-LOCAL', '1.0.0-dev', now())
ON CONFLICT (instance_key) DO UPDATE SET
    input_folder = EXCLUDED.input_folder,
    host_name    = EXCLUDED.host_name,
    app_version  = EXCLUDED.app_version,
    reported_at  = now();

-- Nodos de prueba asignados a Alfa con SQL en localhost.
UPDATE a24c.database_nodes
SET
    server_name = 'localhost',
    is_active   = 1
WHERE restore_target_id = (SELECT id FROM a24c.restore_targets WHERE name = 'Alfa');

-- Asegurar nodo de prueba principal para restore_local.
UPDATE a24c.database_nodes
SET restore_target_id = (SELECT id FROM a24c.restore_targets WHERE name = 'Alfa')
WHERE node_subnode_key = 'GENERICA-TEST';
