-- ============================================================
-- Compliance 카테고리 다중언어 마이그레이션 스크립트
-- ============================================================

-- 1. 카테고리 마스터 테이블 생성
CREATE TABLE IF NOT EXISTS std_snp_data.compliance_category (
    category_code VARCHAR(50) PRIMARY KEY,
    indicator_type VARCHAR(20) NOT NULL,
    sort_order INTEGER NOT NULL
);

-- 2. 카테고리 다중언어 테이블 생성
CREATE TABLE IF NOT EXISTS std_snp_data.compliance_category_lang (
    category_code VARCHAR(50) NOT NULL,
    lang_code VARCHAR(5) NOT NULL,
    category_name VARCHAR(200) NOT NULL,
    PRIMARY KEY (category_code, lang_code)
);

-- 3. 카테고리 마스터 데이터 삽입
INSERT INTO std_snp_data.compliance_category (category_code, indicator_type, sort_order) VALUES
    ('SANCTIONS_SHIP_US_OFAC', 'SHIP', 1),
    ('SANCTIONS_OWNERSHIP_US_OFAC', 'SHIP', 2),
    ('SANCTIONS_SHIP_NON_US', 'SHIP', 3),
    ('SANCTIONS_OWNERSHIP_NON_US', 'SHIP', 4),
    ('SANCTIONS_FATF', 'SHIP', 5),
    ('SANCTIONS_OTHER', 'SHIP', 6),
    ('PORT_CALLS', 'SHIP', 7),
    ('STS_ACTIVITY', 'SHIP', 8),
    ('SUSPICIOUS_BEHAVIOR', 'SHIP', 9),
    ('OWNERSHIP_SCREENING', 'SHIP', 10),
    ('COMPLIANCE_SCREENING_HISTORY', 'SHIP', 11),
    ('US_TREASURY_SANCTIONS', 'COMPANY', 1),
    ('NON_US_SANCTIONS', 'COMPANY', 2),
    ('FATF_JURISDICTION', 'COMPANY', 3),
    ('PARENT_COMPANY', 'COMPANY', 4),
    ('OVERALL_COMPLIANCE_STATUS', 'COMPANY', 5),
    ('RELATED_SCREENING', 'COMPANY', 6),
    ('COMPANY_COMPLIANCE_HISTORY', 'COMPANY', 7);

-- 4. 카테고리 다중언어 데이터 삽입 (EN)
INSERT INTO std_snp_data.compliance_category_lang (category_code, lang_code, category_name) VALUES
    ('SANCTIONS_SHIP_US_OFAC', 'EN', 'Sanctions – Ship (US OFAC)'),
    ('SANCTIONS_OWNERSHIP_US_OFAC', 'EN', 'Sanctions – Ownership (US OFAC)'),
    ('SANCTIONS_SHIP_NON_US', 'EN', 'Sanctions – Ship (Non-US)'),
    ('SANCTIONS_OWNERSHIP_NON_US', 'EN', 'Sanctions – Ownership (Non-US)'),
    ('SANCTIONS_FATF', 'EN', 'Sanctions – FATF'),
    ('SANCTIONS_OTHER', 'EN', 'Sanctions – Other'),
    ('PORT_CALLS', 'EN', 'Port Calls'),
    ('STS_ACTIVITY', 'EN', 'STS Activity'),
    ('SUSPICIOUS_BEHAVIOR', 'EN', 'Suspicious Behavior'),
    ('OWNERSHIP_SCREENING', 'EN', 'Ownership Screening'),
    ('COMPLIANCE_SCREENING_HISTORY', 'EN', 'Compliance Screening History'),
    ('US_TREASURY_SANCTIONS', 'EN', 'US Treasury Sanctions'),
    ('NON_US_SANCTIONS', 'EN', 'Non-US Sanctions'),
    ('FATF_JURISDICTION', 'EN', 'FATF Jurisdiction'),
    ('PARENT_COMPANY', 'EN', 'Parent Company'),
    ('OVERALL_COMPLIANCE_STATUS', 'EN', 'Overall Compliance Status'),
    ('RELATED_SCREENING', 'EN', 'Related Screening'),
    ('COMPANY_COMPLIANCE_HISTORY', 'EN', 'Compliance Screening Change History');

-- 5. 카테고리 다중언어 데이터 삽입 (KO)
INSERT INTO std_snp_data.compliance_category_lang (category_code, lang_code, category_name) VALUES
    ('SANCTIONS_SHIP_US_OFAC', 'KO', '제재 – 선박 (US OFAC)'),
    ('SANCTIONS_OWNERSHIP_US_OFAC', 'KO', '제재 – 소유권 (US OFAC)'),
    ('SANCTIONS_SHIP_NON_US', 'KO', '제재 – 선박 (비미국)'),
    ('SANCTIONS_OWNERSHIP_NON_US', 'KO', '제재 – 소유권 (비미국)'),
    ('SANCTIONS_FATF', 'KO', '제재 – FATF'),
    ('SANCTIONS_OTHER', 'KO', '제재 – 기타'),
    ('PORT_CALLS', 'KO', '입항 이력'),
    ('STS_ACTIVITY', 'KO', 'STS 활동'),
    ('SUSPICIOUS_BEHAVIOR', 'KO', '의심 행위'),
    ('OWNERSHIP_SCREENING', 'KO', '소유권 심사'),
    ('COMPLIANCE_SCREENING_HISTORY', 'KO', '컴플라이언스 심사 이력'),
    ('US_TREASURY_SANCTIONS', 'KO', '미국 재무부 제재'),
    ('NON_US_SANCTIONS', 'KO', '비미국 제재'),
    ('FATF_JURISDICTION', 'KO', 'FATF 관할지역'),
    ('PARENT_COMPANY', 'KO', '모회사'),
    ('OVERALL_COMPLIANCE_STATUS', 'KO', '종합 컴플라이언스 상태'),
    ('RELATED_SCREENING', 'KO', '관련 심사'),
    ('COMPANY_COMPLIANCE_HISTORY', 'KO', '컴플라이언스 심사 변경 이력');

-- 6. compliance_indicator 테이블: category → category_code 변환
-- 먼저 컬럼 추가
ALTER TABLE std_snp_data.compliance_indicator ADD COLUMN category_code VARCHAR(50);

-- 기존 category 값을 category_code로 매핑
UPDATE std_snp_data.compliance_indicator SET category_code = 'SANCTIONS_SHIP_US_OFAC' WHERE category = 'Sanctions – Ship (US OFAC)';
UPDATE std_snp_data.compliance_indicator SET category_code = 'SANCTIONS_OWNERSHIP_US_OFAC' WHERE category = 'Sanctions – Ownership (US OFAC)';
UPDATE std_snp_data.compliance_indicator SET category_code = 'SANCTIONS_SHIP_NON_US' WHERE category = 'Sanctions – Ship (Non-US)';
UPDATE std_snp_data.compliance_indicator SET category_code = 'SANCTIONS_OWNERSHIP_NON_US' WHERE category = 'Sanctions – Ownership (Non-US)';
UPDATE std_snp_data.compliance_indicator SET category_code = 'SANCTIONS_FATF' WHERE category = 'Sanctions – FATF';
UPDATE std_snp_data.compliance_indicator SET category_code = 'SANCTIONS_OTHER' WHERE category = 'Sanctions – Other';
UPDATE std_snp_data.compliance_indicator SET category_code = 'PORT_CALLS' WHERE category = 'Port Calls';
UPDATE std_snp_data.compliance_indicator SET category_code = 'STS_ACTIVITY' WHERE category = 'STS Activity';
UPDATE std_snp_data.compliance_indicator SET category_code = 'SUSPICIOUS_BEHAVIOR' WHERE category = 'Suspicious Behavior';
UPDATE std_snp_data.compliance_indicator SET category_code = 'OWNERSHIP_SCREENING' WHERE category = 'Ownership Screening';
UPDATE std_snp_data.compliance_indicator SET category_code = 'COMPLIANCE_SCREENING_HISTORY' WHERE category = 'Compliance Screening History';
UPDATE std_snp_data.compliance_indicator SET category_code = 'US_TREASURY_SANCTIONS' WHERE category = 'US Treasury Sanctions';
UPDATE std_snp_data.compliance_indicator SET category_code = 'NON_US_SANCTIONS' WHERE category = 'Non-US Sanctions';
UPDATE std_snp_data.compliance_indicator SET category_code = 'FATF_JURISDICTION' WHERE category = 'FATF Jurisdiction';
UPDATE std_snp_data.compliance_indicator SET category_code = 'PARENT_COMPANY' WHERE category = 'Parent Company';
UPDATE std_snp_data.compliance_indicator SET category_code = 'OVERALL_COMPLIANCE_STATUS' WHERE category = 'Overall Compliance Status';
UPDATE std_snp_data.compliance_indicator SET category_code = 'RELATED_SCREENING' WHERE category = 'Related Screening';
UPDATE std_snp_data.compliance_indicator SET category_code = 'COMPANY_COMPLIANCE_HISTORY' WHERE category = 'Compliance Screening Change History';

-- category_code NOT NULL 설정
ALTER TABLE std_snp_data.compliance_indicator ALTER COLUMN category_code SET NOT NULL;

-- 기존 category 컬럼 삭제
ALTER TABLE std_snp_data.compliance_indicator DROP COLUMN category;
