-- 완료 신청 관련 필드 추가
-- 마이그레이션: 019_add_completion_request_fields.sql

DO $$
BEGIN
    -- 완료 신청 관련 필드들 추가
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'issues' AND column_name = 'completion_requested_at') THEN
        ALTER TABLE issues ADD COLUMN completion_requested_at TIMESTAMP WITH TIME ZONE;
    END IF;
    
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'issues' AND column_name = 'completion_requested_by_id') THEN
        ALTER TABLE issues ADD COLUMN completion_requested_by_id INTEGER REFERENCES users(id);
    END IF;
    
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'issues' AND column_name = 'completion_photo_path') THEN
        ALTER TABLE issues ADD COLUMN completion_photo_path VARCHAR(500);
    END IF;
    
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'issues' AND column_name = 'completion_comment') THEN
        ALTER TABLE issues ADD COLUMN completion_comment TEXT;
    END IF;
    
    -- 마이그레이션 로그 기록
    INSERT INTO migration_log (migration_file, executed_at, status, notes)
    VALUES ('019_add_completion_request_fields.sql', NOW(), 'SUCCESS', 'Added completion request fields: completion_requested_at, completion_requested_by_id, completion_photo_path, completion_comment');
    
    RAISE NOTICE '✅ 완료 신청 관련 필드 추가 완료';
    RAISE NOTICE '📝 완료 신청 필드 마이그레이션 완료 - 019_add_completion_request_fields.sql';
    
EXCEPTION
    WHEN OTHERS THEN
        -- 오류 발생 시 로그 기록
        INSERT INTO migration_log (migration_file, executed_at, status, notes)
        VALUES ('019_add_completion_request_fields.sql', NOW(), 'FAILED', 'Error: ' || SQLERRM);
        
        RAISE EXCEPTION '❌ 마이그레이션 실패: %', SQLERRM;
END $$;
