-- =============================================================================
-- Risk & Compliance Indicator 테이블에 column_name 매핑
-- 목적: flctn_col_nm (이력 테이블 변동컬럼명) → indicator 테이블 JOIN 가능하도록
-- 참고: INSERT SQL의 field_key 기준으로 매핑 (risk_indicator.sql, compliance_indicator.sql)
-- =============================================================================

-- ※ column_name 컬럼이 이미 INSERT SQL에 NULL로 포함되어 있으므로
--    ALTER TABLE은 column_name 컬럼이 없는 경우에만 실행
-- ALTER TABLE std_snp_data.risk_indicator ADD COLUMN column_name varchar(100);
-- ALTER TABLE std_snp_data.compliance_indicator ADD COLUMN column_name varchar(100);


-- 1. UPDATE: Risk Indicator
-- -----------------------------------------------------------------------------

-- AIS
UPDATE std_snp_data.risk_indicator SET column_name = 'ais_notrcv_elps_days'   WHERE field_key = 'Time since last seen on AIS';
UPDATE std_snp_data.risk_indicator SET column_name = 'ais_lwrnk_days'         WHERE field_key = 'Days under AIS coverage (last 12 months)';
UPDATE std_snp_data.risk_indicator SET column_name = 'mmsi_anom_message'      WHERE field_key = 'Anomalous AIS Messages from MMSI (last 12 months)';
UPDATE std_snp_data.risk_indicator SET column_name = 'ais_up_imo_desc'        WHERE field_key = 'IMO number transmitted correctly in AIS';
UPDATE std_snp_data.risk_indicator SET column_name = 'othr_ship_nm_voy_yn'    WHERE field_key = 'Sailing under name transmitted on AIS';

-- PORT_CALLS
UPDATE std_snp_data.risk_indicator SET column_name = 'port_prtcll'            WHERE field_key = 'Port calls (last 12 months)';
UPDATE std_snp_data.risk_indicator SET column_name = 'recent_sanction_prtcll' WHERE field_key = 'Most recent sanctioned port call';
UPDATE std_snp_data.risk_indicator SET column_name = 'port_risk'              WHERE field_key = 'Highest ECR risk port call (last 12 months)';

-- ASSOCIATED_WITH_RUSSIA
UPDATE std_snp_data.risk_indicator SET column_name = 'rss_ownr_reg'           WHERE field_key = 'Russian registration or ownership since February 2022';
UPDATE std_snp_data.risk_indicator SET column_name = 'rss_port_call'          WHERE field_key = 'Russian port calls since February 2022';
UPDATE std_snp_data.risk_indicator SET column_name = 'rss_sts'                WHERE field_key = 'Russian tanker STS since December 2022';

-- BEHAVIOURAL_RISK
UPDATE std_snp_data.risk_indicator SET column_name = 'recent_dark_actv'       WHERE field_key = 'Most recent suspicious behavior detected';
UPDATE std_snp_data.risk_indicator SET column_name = 'sts_job'                WHERE field_key = 'Ship-to-Ship operations (last 12 months)';
UPDATE std_snp_data.risk_indicator SET column_name = 'draft_chg'              WHERE field_key = 'Draught changes (last 12 months)';
UPDATE std_snp_data.risk_indicator SET column_name = 'drift_chg'              WHERE field_key = 'Drifting high seas (last 12 months)';
UPDATE std_snp_data.risk_indicator SET column_name = 'ilgl_fshr_viol'         WHERE field_key = 'Illegal Unreported or Unregulated (IUU) Fishing Violation';

-- SAFETY_SECURITY_AND_INSPECTIONS
UPDATE std_snp_data.risk_indicator SET column_name = 'risk_event'             WHERE field_key = 'Casualty & risk events (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'fltsfty'                WHERE field_key = 'Fleet casualty & risk (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'vslage'                 WHERE field_key = 'Age of ship (compared to peer group average)';
UPDATE std_snp_data.risk_indicator SET column_name = 'psc_inspection'         WHERE field_key = 'Inspection (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'psc_inspection_elps_hr' WHERE field_key = 'Time since last inspection';
UPDATE std_snp_data.risk_indicator SET column_name = 'psc_defect'             WHERE field_key = 'PSC defects (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'psc_detained'           WHERE field_key = 'PSC detentions (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'now_smgrc_evdc'         WHERE field_key = 'Current Safety Management Certificate inspected';
UPDATE std_snp_data.risk_indicator SET column_name = 'flt_psc'                WHERE field_key = 'Fleet PSC detentions (last 3 years)';

-- FLAG_RISK
UPDATE std_snp_data.risk_indicator SET column_name = 'ntnlty_chg'            WHERE field_key = 'Flag changes (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'ntnlty_prs_mou_perf'   WHERE field_key = 'Flag Paris MOU performance';
UPDATE std_snp_data.risk_indicator SET column_name = 'ntnlty_tky_mou_perf'   WHERE field_key = 'Flag Tokyo MOU performance';
UPDATE std_snp_data.risk_indicator SET column_name = 'ntnlty_uscg_mou_perf'  WHERE field_key = 'Flag US Coastguard MOU performance';
UPDATE std_snp_data.risk_indicator SET column_name = 'uscg_excl_ship_cert'   WHERE field_key = 'Flag US Coastguard Qualship 21';
UPDATE std_snp_data.risk_indicator SET column_name = 'risk_data_maint'       WHERE field_key = 'Risk Data Maintained For Vessel';

-- OWNER_AND_CLASSIFICATION
UPDATE std_snp_data.risk_indicator SET column_name = 'now_clfic'             WHERE field_key = 'Classification Society';
UPDATE std_snp_data.risk_indicator SET column_name = 'clfic_status_chg'      WHERE field_key = 'Class status changes (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'spc_inspection_ovdue'  WHERE field_key = 'Special survey overdue';
UPDATE std_snp_data.risk_indicator SET column_name = 'pni_insrnc'            WHERE field_key = 'P&I club check';
UPDATE std_snp_data.risk_indicator SET column_name = 'ship_nm_chg'           WHERE field_key = 'Name changes (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'docc_chg'              WHERE field_key = 'DOC company changes (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'gbo_chg'               WHERE field_key = 'Group owner changes (last 3 years)';
UPDATE std_snp_data.risk_indicator SET column_name = 'ownr_unk'              WHERE field_key = 'Ownership unknown';
UPDATE std_snp_data.risk_indicator SET column_name = 'sngl_ship_voy'         WHERE field_key = 'Single-ship fleet (technical manager)';


-- 2. UPDATE: Compliance Indicator - SHIP
-- -----------------------------------------------------------------------------
-- ※ DB 컬럼이 없는 지표 (Suspicious Behavior, Ownership Screening 등)는 매핑하지 않음

-- Sanctions - Ship (US OFAC)
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ofac_sanction_list'         WHERE field_key = 'Ship on OFAC Sanctions List (SDN)'            AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ofac_non_sdn_sanction_list' WHERE field_key = 'Ship on OFAC Consolidated (Non-SDN) List'     AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ofac_cutn_list'             WHERE field_key = 'Ship on OFAC Advisory List'                   AND indicator_type = 'SHIP';

-- Sanctions - Ownership (US OFAC)
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_ofac_sanction_list'                 WHERE field_key = 'Ownership on OFAC Sanctions List'                      AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_ofcs_sanction_list'                 WHERE field_key = 'Ownership on OFAC SSI List'                            AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_ofac_sanction_country'              WHERE field_key = 'Ownership in OFAC Sanctioned Country'                  AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_ofac_sanction_hstry'                WHERE field_key = 'Historical Ownership in OFAC Sanctioned Country'       AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_prnt_company_ofac_sanction_country' WHERE field_key = 'Parent Company in OFAC Sanctioned Country'              AND indicator_type = 'SHIP';

-- Sanctions - Ship (Non-US)
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_un_sanction_list'    WHERE field_key = 'Ship on UN Security Council Sanctions List' AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_eu_sanction_list'    WHERE field_key = 'Ship on EU Commission Sanctions List'       AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_swi_sanction_list'   WHERE field_key = 'Ship on Swiss SECO Sanctions List'          AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_bes_sanction_list'   WHERE field_key = 'Ship on HM Treasury (BES) Sanctions List'   AND indicator_type = 'SHIP';

-- Sanctions - Ownership (Non-US)
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_un_sanction_list'  WHERE field_key = 'Ownership on UN Security Council Sanctions List'      AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_eu_sanction_list'  WHERE field_key = 'Ownership on EU Commission Sanctions List'            AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_swi_sanction_list' WHERE field_key = 'Ownership on Swiss SECO Sanctions List'               AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_bes_sanction_list' WHERE field_key = 'Ownership on HM Treasury (BES) Sanctions List'        AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_can_sanction_list' WHERE field_key = 'Ownership on Government of Canada Sanctions List'     AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_aus_sanction_list' WHERE field_key = 'Ownership on Australian DFAT Sanctions List'          AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_uae_sanction_list' WHERE field_key = 'Ownership on UAE Sanctions List'                      AND indicator_type = 'SHIP';

-- Sanctions - FATF
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_fatf_rgl_zone'                WHERE field_key = 'Ownership in FATF High-risk or Non-cooperative Jurisdiction'        AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_prnt_company_fatf_rgl_zone'   WHERE field_key = 'Parent Company in FATF High-risk or Non-cooperative Jurisdiction'   AND indicator_type = 'SHIP';

-- Sanctions - Other
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_ownr_prnt_company_ncmplnc'         WHERE field_key = 'Parent Company Noncompliant'                          AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_flg_sanction_country'               WHERE field_key = 'Flag Country Sanctioned'                              AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_flg_sanction_country_hstry'         WHERE field_key = 'Historical Flag Country Sanctioned'                   AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_scrty_lgl_dspt_event'               WHERE field_key = 'Security and Legal Dispute Event (Last 3 Years)'     AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_flg_dspt'                           WHERE field_key = 'Flag (MMSI, Call Sign) False or Flag Unknown'        AND indicator_type = 'SHIP';

-- Port Calls
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_sanction_country_prtcll_last_thr_m'    WHERE field_key = 'Port Call Last 3 Months to Sanctioned Country'    AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_sanction_country_prtcll_last_six_m'    WHERE field_key = 'Port Call Last 180 Days to Sanctioned Country'    AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_sanction_country_prtcll_last_twelve_m' WHERE field_key = 'Port Call Last 12 Months to Sanctioned Country'   AND indicator_type = 'SHIP';

-- STS Activity
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_sts_prtnr_non_compliance_twelve_m' WHERE field_key = 'STS Activity – Partner Ship Compliance Status' AND indicator_type = 'SHIP';

-- Suspicious Behavior
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_dark_actv_ind'  WHERE field_key = 'Dark for Extended Period in Watched Area (Severe)'  AND indicator_type = 'SHIP';
UPDATE std_snp_data.compliance_indicator SET column_name = 'ship_dtld_info_ntmntd' WHERE field_key = 'Not Seen 7 Days Near Sanctioned/Sensitive Country' AND indicator_type = 'SHIP';

-- Compliance Screening History
UPDATE std_snp_data.compliance_indicator SET column_name = 'lgl_snths_sanction' WHERE field_key = 'Overall Compliance Change History' AND indicator_type = 'SHIP';


-- 3. UPDATE: Compliance Indicator - COMPANY
-- -----------------------------------------------------------------------------

-- US Treasury Sanctions
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_ofac_sanction_list'        WHERE field_key = 'Company on OFAC Entity List'                               AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_ofac_non_sdn_sanction_list' WHERE field_key = 'Company on OFAC Non-SDN Entity List'                      AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_ofacssi_sanction_list'     WHERE field_key = 'Company on OFAC SSI Entity List'                           AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_ofac_sanction_country'     WHERE field_key = 'Company in US Treasury OFAC Sanctioned Country'            AND indicator_type = 'COMPANY';

-- Non-US Sanctions
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_un_sanction_list'          WHERE field_key = 'Company on UN Security Council Entity List'                AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_eu_sanction_list'          WHERE field_key = 'Company on EU Entity List'                                 AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_bes_sanction_list'         WHERE field_key = 'Company on HM Treasury (BES) Entity List'                  AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_can_sanction_list'         WHERE field_key = 'Company on Canadian Entity List'                           AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_aus_sanction_list'         WHERE field_key = 'Company on Australian DFAT Entity List'                    AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_uae_sanction_list'         WHERE field_key = 'Company on UAE Entity List'                                AND indicator_type = 'COMPANY';
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_swiss_sanction_list'       WHERE field_key = 'Company on Swiss SECO Entity List'                         AND indicator_type = 'COMPANY';

-- FATF Jurisdiction
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_fatf_cmptnc_country'      WHERE field_key = 'Company in FATF High-risk & Non-cooperative Jurisdiction'  AND indicator_type = 'COMPANY';

-- Parent Company
UPDATE std_snp_data.compliance_indicator SET column_name = 'prnt_company_compliance_risk'     WHERE field_key = 'Parent Company Compliance Risk'                            AND indicator_type = 'COMPANY';

-- Compliance Screening Change History
UPDATE std_snp_data.compliance_indicator SET column_name = 'company_snths_compliance_status'   WHERE field_key = 'Historical Compliance Change Date (Company)'               AND indicator_type = 'COMPANY';


-- 4. 검증 쿼리
-- -----------------------------------------------------------------------------

-- 매핑된 항목 수 확인
SELECT 'risk_indicator' as table_name,
    COUNT(*) as total,
    COUNT(column_name) as mapped,
    COUNT(*) - COUNT(column_name) as unmapped
FROM std_snp_data.risk_indicator
UNION ALL
SELECT 'compliance_indicator (SHIP)',
    COUNT(*), COUNT(column_name), COUNT(*) - COUNT(column_name)
FROM std_snp_data.compliance_indicator WHERE indicator_type = 'SHIP'
UNION ALL
SELECT 'compliance_indicator (COMPANY)',
    COUNT(*), COUNT(column_name), COUNT(*) - COUNT(column_name)
FROM std_snp_data.compliance_indicator WHERE indicator_type = 'COMPANY';

-- 매핑 안 된 항목 확인 (DB 컬럼 없는 지표 = 정상적으로 NULL)
SELECT indicator_id, field_key, column_name
FROM std_snp_data.risk_indicator WHERE column_name IS NULL;

SELECT indicator_id, field_key, indicator_type, column_name
FROM std_snp_data.compliance_indicator WHERE column_name IS NULL;

-- 전체 매핑 확인 (다국어 포함)
SELECT ri.indicator_id, ri.field_key, ri.column_name, ril.field_name
FROM std_snp_data.risk_indicator ri
LEFT JOIN std_snp_data.risk_indicator_lang ril
    ON ri.indicator_id = ril.indicator_id AND ril.lang_code = 'KO'
ORDER BY ri.indicator_id;

SELECT ci.indicator_id, ci.field_key, ci.indicator_type, ci.column_name, cil.field_name
FROM std_snp_data.compliance_indicator ci
LEFT JOIN std_snp_data.compliance_indicator_lang cil
    ON ci.indicator_id = cil.indicator_id AND cil.lang_code = 'KO'
ORDER BY ci.indicator_type, ci.indicator_id;
