-- Issue #86: 巡检管理核心（总览/轨迹/台账/设备/工单）

-- 1. 巡检设备台账
CREATE TABLE IF NOT EXISTS patrol_device (
    id              BIGSERIAL PRIMARY KEY,
    device_no       VARCHAR(50) UNIQUE,
    device_name     VARCHAR(100) NOT NULL,
    device_type     VARCHAR(30) NOT NULL,        -- valve/meter/hydrant/pump/sensor
    location        VARCHAR(300),
    lng             DOUBLE PRECISION,
    lat             DOUBLE PRECISION,
    status          VARCHAR(20) DEFAULT 'normal', -- normal/abnormal/offline/maintenance
    area            VARCHAR(50),
    point_seq       INT DEFAULT 0,
    remark          TEXT,
    last_maintenance_date TIMESTAMPTZ,
    created_at      TIMESTAMPTZ DEFAULT NOW(),
    updated_at      TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_patrol_device_area ON patrol_device(area);
CREATE INDEX IF NOT EXISTS idx_patrol_device_type ON patrol_device(device_type);
CREATE INDEX IF NOT EXISTS idx_patrol_device_status ON patrol_device(status);

-- 2. GPS轨迹点
CREATE TABLE IF NOT EXISTS patrol_track_point (
    id              BIGSERIAL PRIMARY KEY,
    task_id         BIGINT NOT NULL REFERENCES patrol_task(id),
    worker_id       BIGINT,
    lng             DOUBLE PRECISION NOT NULL,
    lat             DOUBLE PRECISION NOT NULL,
    speed           DOUBLE PRECISION DEFAULT 0,
    accuracy        DOUBLE PRECISION DEFAULT 0,
    recorded_at     TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_track_task ON patrol_track_point(task_id, recorded_at);

-- 3. 巡检工单增强（如果 patrol_work_order 已存在则 ALTER，否则 CREATE）
-- 先检查 patrol_work_order 是否存在
DO $$
BEGIN
    IF NOT EXISTS (SELECT FROM pg_tables WHERE tablename = 'patrol_work_order') THEN
        CREATE TABLE patrol_work_order (
            id              BIGSERIAL PRIMARY KEY,
            order_no        VARCHAR(50) UNIQUE NOT NULL,
            task_id         BIGINT REFERENCES patrol_task(id),
            issue_type      VARCHAR(30) NOT NULL,     -- leak/damage/abnormal/pollution/other
            description     TEXT,
            photos          TEXT,                      -- JSON array
            severity        VARCHAR(10) DEFAULT 'medium', -- low/medium/high/critical
            assignee_id     BIGINT,
            assignee_name   VARCHAR(50),
            status          VARCHAR(20) DEFAULT 'pending', -- pending/assigned/processing/resolved/closed
            location        VARCHAR(300),
            lng             DOUBLE PRECISION,
            lat             DOUBLE PRECISION,
            resolution      TEXT,
            created_at      TIMESTAMPTZ DEFAULT NOW(),
            resolved_at     TIMESTAMPTZ,
            closed_at       TIMESTAMPTZ
        );
        CREATE INDEX IF NOT EXISTS idx_pwo_status ON patrol_work_order(status);
        CREATE INDEX IF NOT EXISTS idx_pwo_assignee ON patrol_work_order(assignee_id);
    END IF;
END $$;

-- 4. 设备巡检点检记录扩展（给 patrol_record 加 device_status 字段）
ALTER TABLE patrol_record ADD COLUMN IF NOT EXISTS device_status VARCHAR(20) DEFAULT 'normal';
ALTER TABLE patrol_record ADD COLUMN IF NOT EXISTS remark TEXT;
