-- ============================
-- Supabase PostgreSQL Schema
-- ============================

-- Table 1: daily_ra (日次リスクアセスメント)
CREATE TABLE IF NOT EXISTS daily_ra (
    id SERIAL PRIMARY KEY,
    work_date DATE NOT NULL UNIQUE,
    work_content TEXT,
    hazard_points TEXT,
    general_comments TEXT,
    risk_identification TEXT,
    mitigation_measures TEXT,
    inspection_items TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Table 2: rule_base (自動生成されたルール)
CREATE TABLE IF NOT EXISTS rule_base (
    id SERIAL PRIMARY KEY,
    daily_ra_id INTEGER NOT NULL REFERENCES daily_ra(id) ON DELETE CASCADE,
    object1 VARCHAR(100),
    object2 VARCHAR(100),
    risk TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Indexes for better performance
CREATE INDEX IF NOT EXISTS idx_daily_ra_work_date ON daily_ra(work_date);
CREATE INDEX IF NOT EXISTS idx_rule_base_daily_ra_id ON rule_base(daily_ra_id);

-- Comments (optional, for documentation)
COMMENT ON TABLE daily_ra IS '日次リスクアセスメントデータ';
COMMENT ON TABLE rule_base IS '自動生成された安全ルール';

