-- Operation-system business tables (design doc §4 / §12 P1).
-- Six ops configuration tables + event attribution columns.

-- 1. station: 工位
CREATE TABLE station (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    code TEXT NOT NULL,
    name TEXT NOT NULL,
    line_code TEXT,
    segment_code TEXT,
    station_type TEXT NOT NULL,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    description TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (code)
);

CREATE INDEX idx_station_line_code ON station(line_code);
CREATE INDEX idx_station_segment_code ON station(segment_code);

-- 2. station_signal: 工位 ↔ 信号绑定
CREATE TABLE station_signal (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    station_id UUID NOT NULL REFERENCES station(id) ON DELETE CASCADE,
    signal_role TEXT NOT NULL,
    point_id UUID REFERENCES point(id) ON DELETE SET NULL,
    derived_from_role TEXT,
    invert_value BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (station_id, signal_role)
);

CREATE INDEX idx_station_signal_point_id ON station_signal(point_id);

-- 3. process_segment: 流程段
CREATE TABLE process_segment (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    code TEXT NOT NULL,
    name TEXT NOT NULL,
    segment_type TEXT NOT NULL,
    line_code TEXT,
    priority INTEGER NOT NULL DEFAULT 0,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    mode TEXT NOT NULL DEFAULT 'disabled',
    require_manual_ack_after_fault BOOLEAN NOT NULL DEFAULT TRUE,
    description TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (code)
);

CREATE INDEX idx_process_segment_line_code ON process_segment(line_code);
CREATE INDEX idx_process_segment_enabled ON process_segment(enabled);

-- 4. segment_step: 段步骤
CREATE TABLE segment_step (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    segment_id UUID NOT NULL REFERENCES process_segment(id) ON DELETE CASCADE,
    step_no INTEGER NOT NULL,
    step_code TEXT NOT NULL,
    action_kind TEXT NOT NULL,
    target_equipment_id UUID REFERENCES equipment(id) ON DELETE SET NULL,
    target_station_id UUID REFERENCES station(id) ON DELETE SET NULL,
    confirm_signal_role TEXT,
    confirm_point_id UUID REFERENCES point(id) ON DELETE SET NULL,
    expected_value BOOLEAN NOT NULL DEFAULT TRUE,
    timeout_ms INTEGER NOT NULL DEFAULT 30000 CHECK (timeout_ms > 0),
    command_role TEXT,
    stop_command_role TEXT,
    pulse_ms INTEGER CHECK (pulse_ms IS NULL OR pulse_ms > 0),
    hold_until_confirm BOOLEAN NOT NULL DEFAULT FALSE,
    cancel_on_fault BOOLEAN NOT NULL DEFAULT TRUE,
    next_step_no_on_success INTEGER,
    next_step_no_on_failure INTEGER,
    on_timeout TEXT NOT NULL DEFAULT 'fault',
    description TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (segment_id, step_no)
);

CREATE INDEX idx_segment_step_segment_id ON segment_step(segment_id);

-- 5. segment_interlock: 段联锁
CREATE TABLE segment_interlock (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    segment_id UUID NOT NULL REFERENCES process_segment(id) ON DELETE CASCADE,
    applies_to TEXT NOT NULL,
    rule_kind TEXT NOT NULL,
    point_id UUID REFERENCES point(id) ON DELETE SET NULL,
    station_id UUID REFERENCES station(id) ON DELETE SET NULL,
    equipment_id UUID REFERENCES equipment(id) ON DELETE SET NULL,
    expected_value BOOLEAN,
    description TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_segment_interlock_segment_id ON segment_interlock(segment_id);

-- 6. segment_resource: 段资源声明
CREATE TABLE segment_resource (
    segment_id UUID NOT NULL REFERENCES process_segment(id) ON DELETE CASCADE,
    resource_key TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    PRIMARY KEY (segment_id, resource_key)
);

-- 7. event attribution: subject_type / subject_id (design doc §4.2.8)
ALTER TABLE event
    ADD COLUMN subject_type TEXT,
    ADD COLUMN subject_id UUID;

CREATE INDEX idx_event_subject ON event(subject_type, subject_id);
