-- =====================================================
-- HGZero Meeting Service - Initial Schema
-- Version: V1
-- Description: 초기 데이터베이스 스키마 생성
-- =====================================================

-- Note: 기존 테이블이 있으면 유지하고, 없으면 새로 생성
-- CREATE TABLE IF NOT EXISTS를 사용하여 안전하게 처리

-- 2. Templates 테이블
CREATE TABLE IF NOT EXISTS templates (
    template_id VARCHAR(50) PRIMARY KEY,
    name VARCHAR(200) NOT NULL,
    description TEXT,
    category VARCHAR(50) NOT NULL,
    sections TEXT,
    is_public BOOLEAN NOT NULL DEFAULT true,
    created_by VARCHAR(50) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 3. Meetings 테이블
CREATE TABLE IF NOT EXISTS meetings (
    meeting_id VARCHAR(50) PRIMARY KEY,
    title VARCHAR(200) NOT NULL,
    purpose VARCHAR(500),
    description TEXT,
    scheduled_at TIMESTAMP NOT NULL,
    end_time TIMESTAMP,
    location VARCHAR(200),
    started_at TIMESTAMP,
    ended_at TIMESTAMP,
    status VARCHAR(20) NOT NULL DEFAULT 'SCHEDULED',
    organizer_id VARCHAR(50) NOT NULL,
    template_id VARCHAR(50),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_meetings_template FOREIGN KEY (template_id) REFERENCES templates(template_id)
);

-- 4. Meeting Participants 테이블 (기존 V2 마이그레이션 통합)
CREATE TABLE IF NOT EXISTS meeting_participants (
    meeting_id VARCHAR(50) NOT NULL,
    user_id VARCHAR(100) NOT NULL,
    invitation_status VARCHAR(20) DEFAULT 'PENDING',
    attended BOOLEAN DEFAULT false,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (meeting_id, user_id),
    CONSTRAINT fk_participants_meeting FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id) ON DELETE CASCADE
);

-- 5. Sessions 테이블
CREATE TABLE IF NOT EXISTS sessions (
    session_id VARCHAR(50) PRIMARY KEY,
    meeting_id VARCHAR(50) NOT NULL,
    minutes_id VARCHAR(50),
    started_by VARCHAR(50) NOT NULL,
    started_at TIMESTAMP NOT NULL,
    ended_at TIMESTAMP,
    status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_sessions_meeting FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id)
);

-- 6. Minutes 테이블
CREATE TABLE IF NOT EXISTS minutes (
    minutes_id VARCHAR(50) PRIMARY KEY,
    meeting_id VARCHAR(50) NOT NULL,
    title VARCHAR(200) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
    version INTEGER NOT NULL DEFAULT 1,
    created_by VARCHAR(50) NOT NULL,
    finalized_by VARCHAR(50),
    finalized_at TIMESTAMP,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_minutes_meeting FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id)
);

-- 7. Minutes Sections 테이블
CREATE TABLE IF NOT EXISTS minutes_sections (
    section_id VARCHAR(50) PRIMARY KEY,
    minutes_id VARCHAR(50) NOT NULL,
    type VARCHAR(50) NOT NULL,
    title VARCHAR(200) NOT NULL,
    content TEXT,
    "order" INTEGER DEFAULT 0,
    verified BOOLEAN NOT NULL DEFAULT false,
    locked BOOLEAN NOT NULL DEFAULT false,
    locked_by VARCHAR(50),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_sections_minutes FOREIGN KEY (minutes_id) REFERENCES minutes(minutes_id) ON DELETE CASCADE
);

-- 8. Todos 테이블
CREATE TABLE IF NOT EXISTS todos (
    todo_id VARCHAR(50) PRIMARY KEY,
    minutes_id VARCHAR(50),
    meeting_id VARCHAR(50) NOT NULL,
    title VARCHAR(200) NOT NULL,
    description TEXT,
    assignee_id VARCHAR(50) NOT NULL,
    assignee_name VARCHAR(100),
    due_date DATE,
    status VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    priority VARCHAR(20) DEFAULT 'MEDIUM',
    completed_at TIMESTAMP,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_todos_meeting FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id)
);

-- 9. Meeting Analysis 테이블
CREATE TABLE IF NOT EXISTS meeting_analysis (
    analysis_id VARCHAR(50) PRIMARY KEY,
    meeting_id VARCHAR(50) NOT NULL,
    minutes_id VARCHAR(50) NOT NULL,
    agenda_analyses TEXT,
    status VARCHAR(20),
    completed_at TIMESTAMP,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_analysis_meeting FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id),
    CONSTRAINT fk_analysis_minutes FOREIGN KEY (minutes_id) REFERENCES minutes(minutes_id)
);

-- 10. Meeting Keywords 테이블 (ElementCollection)
CREATE TABLE IF NOT EXISTS meeting_keywords (
    analysis_id VARCHAR(50) NOT NULL,
    keyword VARCHAR(255),
    CONSTRAINT fk_keywords_analysis FOREIGN KEY (analysis_id) REFERENCES meeting_analysis(analysis_id) ON DELETE CASCADE
);

-- 11. 인덱스 생성
CREATE INDEX IF NOT EXISTS idx_meetings_organizer ON meetings(organizer_id);
CREATE INDEX IF NOT EXISTS idx_meetings_status ON meetings(status);
CREATE INDEX IF NOT EXISTS idx_meetings_scheduled ON meetings(scheduled_at);

CREATE INDEX IF NOT EXISTS idx_participants_user ON meeting_participants(user_id);

CREATE INDEX IF NOT EXISTS idx_sessions_meeting ON sessions(meeting_id);
CREATE INDEX IF NOT EXISTS idx_sessions_status ON sessions(status);

CREATE INDEX IF NOT EXISTS idx_minutes_meeting ON minutes(meeting_id);
CREATE INDEX IF NOT EXISTS idx_minutes_status ON minutes(status);

CREATE INDEX IF NOT EXISTS idx_sections_minutes ON minutes_sections(minutes_id);
CREATE INDEX IF NOT EXISTS idx_sections_order ON minutes_sections("order");

CREATE INDEX IF NOT EXISTS idx_todos_meeting ON todos(meeting_id);
CREATE INDEX IF NOT EXISTS idx_todos_assignee ON todos(assignee_id);
CREATE INDEX IF NOT EXISTS idx_todos_status ON todos(status);
CREATE INDEX IF NOT EXISTS idx_todos_due_date ON todos(due_date);

CREATE INDEX IF NOT EXISTS idx_analysis_meeting ON meeting_analysis(meeting_id);
CREATE INDEX IF NOT EXISTS idx_analysis_minutes ON meeting_analysis(minutes_id);

-- 12. Sessions에 minutes 외래키 추가 (minutes 테이블 생성 후)
-- 기존 데이터 무결성 이슈로 인해 외래키 제약조건 비활성화
-- ALTER TABLE sessions DROP CONSTRAINT IF EXISTS fk_sessions_minutes;
-- ALTER TABLE sessions ADD CONSTRAINT fk_sessions_minutes
--     FOREIGN KEY (minutes_id) REFERENCES minutes(minutes_id);

-- 13. Todos에 minutes 외래키 추가
-- 기존 데이터 무결성 이슈로 인해 외래키 제약조건 비활성화
-- ALTER TABLE todos DROP CONSTRAINT IF EXISTS fk_todos_minutes;
-- ALTER TABLE todos ADD CONSTRAINT fk_todos_minutes
--     FOREIGN KEY (minutes_id) REFERENCES minutes(minutes_id);

-- 완료 메시지
DO $$
BEGIN
    RAISE NOTICE 'Initial schema created successfully';
END $$;
