-- VMS-Sailor .vmsproj schema v1
-- SQLite portable. Cada proyecto = 1 archivo. Sprint 0.

PRAGMA foreign_keys = ON;

-- ----- Meta del archivo -----------------------------------------------------

CREATE TABLE IF NOT EXISTS schema_version (
    version INTEGER PRIMARY KEY,
    applied_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS meta (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL
);

-- ----- Project (singleton) --------------------------------------------------

CREATE TABLE IF NOT EXISTS project (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    customer TEXT NOT NULL DEFAULT '',
    notes TEXT NOT NULL DEFAULT '',
    systems_enabled_json TEXT NOT NULL,  -- JSON array of SystemId values
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    vmssailor_version TEXT NOT NULL
);

-- ----- Vessel ---------------------------------------------------------------

CREATE TABLE IF NOT EXISTS vessel (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    type TEXT NOT NULL,
    subtype TEXT NOT NULL,
    length_overall_m REAL NOT NULL,
    beam_max_m REAL NOT NULL,
    draft_m REAL NOT NULL,
    displacement_kg REAL,
    silhouette_svg TEXT,
    description TEXT NOT NULL DEFAULT '',
    data_source TEXT NOT NULL DEFAULT 'user_input'
);

CREATE TABLE IF NOT EXISTS deck (
    vessel_id TEXT NOT NULL,
    id TEXT NOT NULL,
    name TEXT NOT NULL,
    z_bl_bottom REAL NOT NULL,
    z_bl_top REAL NOT NULL,
    polygon_xy_json TEXT NOT NULL DEFAULT '[]',
    PRIMARY KEY (vessel_id, id),
    FOREIGN KEY (vessel_id) REFERENCES vessel(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS bulkhead (
    vessel_id TEXT NOT NULL,
    id TEXT NOT NULL,
    name TEXT NOT NULL,
    x_pp REAL NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    PRIMARY KEY (vessel_id, id),
    FOREIGN KEY (vessel_id) REFERENCES vessel(id) ON DELETE CASCADE
);

-- ----- Equipment ------------------------------------------------------------

CREATE TABLE IF NOT EXISTS equipment (
    id TEXT PRIMARY KEY,
    model_ref TEXT NOT NULL,
    tag_prefix TEXT NOT NULL UNIQUE,
    display_name TEXT NOT NULL,
    x_pp REAL NOT NULL,
    y_cl REAL NOT NULL,
    z_bl REAL NOT NULL,
    deck_id TEXT,
    system_id TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    installed INTEGER NOT NULL DEFAULT 1
);

-- ----- Bus / CardInstance / Topology ---------------------------------------

CREATE TABLE IF NOT EXISTS bus (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    protocol TEXT NOT NULL,
    physical_port TEXT NOT NULL,
    baud_rate INTEGER NOT NULL DEFAULT 115200,
    parity TEXT NOT NULL DEFAULT 'N',
    stop_bits INTEGER NOT NULL DEFAULT 1,
    termination INTEGER NOT NULL DEFAULT 1,
    description TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS card_instance (
    id TEXT PRIMARY KEY,
    serial_number TEXT NOT NULL DEFAULT '',
    slot_number INTEGER NOT NULL,
    bus_id TEXT NOT NULL,
    bus_role TEXT NOT NULL,
    modbus_address INTEGER,
    physical_location TEXT NOT NULL DEFAULT '',
    x_pp REAL,
    y_cl REAL,
    z_bl REAL,
    firmware_version TEXT NOT NULL DEFAULT '0.0.0',
    FOREIGN KEY (bus_id) REFERENCES bus(id) ON DELETE CASCADE
);

-- ----- Tag + binding + alarms ----------------------------------------------

CREATE TABLE IF NOT EXISTS tag (
    id TEXT PRIMARY KEY,
    equipment_id TEXT,
    description TEXT NOT NULL DEFAULT '',
    unit_si TEXT NOT NULL DEFAULT 'none',
    range_normal_min REAL,
    range_normal_max REAL,
    quality_default TEXT NOT NULL DEFAULT 'good',
    controllable INTEGER NOT NULL DEFAULT 0,
    control_mode TEXT NOT NULL DEFAULT 'monitor',
    authority_required TEXT NOT NULL DEFAULT 'either',
    protocol TEXT NOT NULL DEFAULT 'modbus_rtu',
    address INTEGER,
    historize INTEGER NOT NULL DEFAULT 1,
    historize_period_s REAL NOT NULL DEFAULT 1.0,
    -- Physical binding inlined (1:1 with tag, optional)
    binding_card_id TEXT,
    binding_channel_type TEXT,
    binding_channel_number INTEGER,
    binding_signal_type TEXT,
    binding_filter TEXT NOT NULL DEFAULT 'none',
    binding_filter_param REAL,
    binding_update_rate_ms INTEGER NOT NULL DEFAULT 100,
    binding_scaling_raw_min REAL,
    binding_scaling_raw_max REAL,
    binding_scaling_eng_min REAL,
    binding_scaling_eng_max REAL
);

CREATE TABLE IF NOT EXISTS alarm_config (
    tag_id TEXT NOT NULL,
    id TEXT NOT NULL,
    threshold REAL NOT NULL,
    operator TEXT NOT NULL,
    priority TEXT NOT NULL,
    hysteresis REAL NOT NULL DEFAULT 0.0,
    delay_seconds REAL NOT NULL DEFAULT 0.0,
    message TEXT NOT NULL DEFAULT '',
    escalation_minutes INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY (tag_id, id),
    FOREIGN KEY (tag_id) REFERENCES tag(id) ON DELETE CASCADE
);

-- ----- Permissive rules + conditions ---------------------------------------

CREATE TABLE IF NOT EXISTS permissive_rule (
    id TEXT PRIMARY KEY,
    action_id TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    on_fail_message TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS permissive_condition (
    rule_id TEXT NOT NULL,
    seq INTEGER NOT NULL,
    tag_ref TEXT NOT NULL,
    operator TEXT NOT NULL,
    threshold REAL,
    threshold_low REAL,
    threshold_high REAL,
    severity TEXT NOT NULL DEFAULT 'fail',
    message_on_fail TEXT NOT NULL DEFAULT '',
    PRIMARY KEY (rule_id, seq),
    FOREIGN KEY (rule_id) REFERENCES permissive_rule(id) ON DELETE CASCADE
);

-- ----- Índices auxiliares --------------------------------------------------

CREATE INDEX IF NOT EXISTS idx_equipment_system ON equipment(system_id);
CREATE INDEX IF NOT EXISTS idx_tag_equipment ON tag(equipment_id);
CREATE INDEX IF NOT EXISTS idx_tag_protocol ON tag(protocol);
CREATE INDEX IF NOT EXISTS idx_card_bus ON card_instance(bus_id);
