-- 삭제 로그 테이블 추가
-- 생성일: 2025-11-08
-- 설명: 부적합 등 엔티티 삭제 시 로그를 보관하기 위한 테이블

CREATE TABLE IF NOT EXISTS deletion_logs (
    id SERIAL PRIMARY KEY,
    entity_type VARCHAR(50) NOT NULL,
    entity_id INTEGER NOT NULL,
    entity_data JSONB NOT NULL,
    deleted_by_id INTEGER NOT NULL REFERENCES users(id),
    deleted_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (NOW() AT TIME ZONE 'Asia/Seoul'),
    reason TEXT
);

-- 인덱스 추가
CREATE INDEX IF NOT EXISTS idx_deletion_logs_entity_type ON deletion_logs(entity_type);
CREATE INDEX IF NOT EXISTS idx_deletion_logs_entity_id ON deletion_logs(entity_id);
CREATE INDEX IF NOT EXISTS idx_deletion_logs_deleted_by ON deletion_logs(deleted_by_id);
CREATE INDEX IF NOT EXISTS idx_deletion_logs_deleted_at ON deletion_logs(deleted_at);

-- 테이블 코멘트
COMMENT ON TABLE deletion_logs IS '엔티티 삭제 로그 - 삭제된 데이터의 백업 및 추적';
COMMENT ON COLUMN deletion_logs.entity_type IS '삭제된 엔티티 타입 (issue, project, daily_work 등)';
COMMENT ON COLUMN deletion_logs.entity_id IS '삭제된 엔티티의 ID';
COMMENT ON COLUMN deletion_logs.entity_data IS '삭제 시점의 엔티티 전체 데이터 (JSON)';
COMMENT ON COLUMN deletion_logs.deleted_by_id IS '삭제 실행자 ID';
COMMENT ON COLUMN deletion_logs.deleted_at IS '삭제 시각 (KST)';
COMMENT ON COLUMN deletion_logs.reason IS '삭제 사유 (선택사항)';
