-- 직접 실행: minutes_sections 테이블 재생성

-- 1. 기존 테이블 삭제
DROP TABLE IF EXISTS minutes_sections CASCADE;

-- 2. 테이블 재생성
CREATE TABLE minutes_sections (
    id VARCHAR(50) PRIMARY KEY,
    minutes_id VARCHAR(50) NOT NULL,
    type VARCHAR(50),
    title VARCHAR(200),
    content TEXT,
    "order" INTEGER,
    verified BOOLEAN DEFAULT FALSE,
    locked BOOLEAN DEFAULT FALSE,
    locked_by VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT fk_minutes_sections_minutes
        FOREIGN KEY (minutes_id) REFERENCES minutes(id)
        ON DELETE CASCADE
);

-- 3. 인덱스 생성
CREATE INDEX idx_minutes_sections_minutes ON minutes_sections(minutes_id);
CREATE INDEX idx_minutes_sections_order ON minutes_sections(minutes_id, "order");
CREATE INDEX idx_minutes_sections_type ON minutes_sections(type);
CREATE INDEX idx_minutes_sections_verified ON minutes_sections(verified);

-- 4. 트리거 생성
DROP TRIGGER IF EXISTS update_minutes_sections_updated_at ON minutes_sections;
CREATE TRIGGER update_minutes_sections_updated_at
    BEFORE UPDATE ON minutes_sections
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

-- 확인
SELECT 'minutes_sections 테이블이 성공적으로 생성되었습니다!' as status;
