-- ============================================================
-- Explicit Select AI relationship metadata for ADB + RDS Views.
-- Redmine: #730
-- ============================================================

WHENEVER SQLERROR EXIT SQL.SQLCODE
SET DEFINE OFF
SET FEEDBACK ON

COMMENT ON TABLE hmm_carrier_assignments_v IS
  'ADB employee-to-carrier assignments from HMM_HR_EMPLOYEES, HMM_ACCESS_GROUPS, and HMM_ACCESS_GROUP_MEMBERS. Join relationship: HMM_CARRIER_ASSIGNMENTS_V.CARRIER_CODE joins HMM_RDS_CARRIERS_V.CARRIER_CODE, HMM_RDS_CARRIER_PERF_V.CARRIER_CODE, and HMM_RDS_CARRIER_LATEST_V.CARRIER_CODE.';

COMMENT ON COLUMN hmm_carrier_assignments_v.EMPLOYEE_CODE IS
  'HMM employee code sourced from HMM_HR_EMPLOYEES.EMPLOYEE_CODE. Example: E1002.';

COMMENT ON COLUMN hmm_carrier_assignments_v.MANAGER_EMPLOYEE_CODE IS
  'Direct manager employee code sourced from HMM_HR_EMPLOYEES. Use E1001 to identify the HMM demo manager team.';

COMMENT ON COLUMN hmm_carrier_assignments_v.CARRIER_CODE IS
  'Join Key and Foreign Key to HMM_RDS_CARRIERS_V(CARRIER_CODE), HMM_RDS_CARRIER_PERF_V(CARRIER_CODE), and HMM_RDS_CARRIER_LATEST_V(CARRIER_CODE).';

COMMENT ON TABLE hmm_rds_carriers_v IS
  'PostgreSQL fictional carrier master exposed through HMM_RDS_PG_LINK. Primary Key: CARRIER_CODE. Join relationship: HMM_RDS_CARRIERS_V.CARRIER_CODE joins HMM_CARRIER_ASSIGNMENTS_V.CARRIER_CODE and HMM_RDS_CARRIER_PERF_V.CARRIER_CODE.';

COMMENT ON COLUMN hmm_rds_carriers_v.CARRIER_CODE IS
  'Primary Key. Join Key to HMM_CARRIER_ASSIGNMENTS_V(CARRIER_CODE), HMM_RDS_CARRIER_PERF_V(CARRIER_CODE), and HMM_RDS_CARRIER_LATEST_V(CARRIER_CODE).';

COMMENT ON TABLE hmm_rds_carrier_perf_v IS
  'PostgreSQL monthly carrier KPI exposed through HMM_RDS_PG_LINK. Composite Primary Key: CARRIER_CODE and PERFORMANCE_MONTH. Foreign Key: CARRIER_CODE references HMM_RDS_CARRIERS_V(CARRIER_CODE). Join relationship: CARRIER_CODE joins HMM_CARRIER_ASSIGNMENTS_V.CARRIER_CODE.';

COMMENT ON COLUMN hmm_rds_carrier_perf_v.CARRIER_CODE IS
  'Foreign Key to HMM_RDS_CARRIERS_V(CARRIER_CODE). Join Key to HMM_CARRIER_ASSIGNMENTS_V(CARRIER_CODE).';

COMMENT ON COLUMN hmm_rds_carrier_perf_v.PERFORMANCE_MONTH IS
  'Part of the composite Primary Key with CARRIER_CODE. First calendar day of the KPI month.';

COMMENT ON TABLE hmm_rds_carrier_latest_v IS
  'Latest PostgreSQL KPI for each fictional carrier exposed through HMM_RDS_PG_LINK. Primary Key: CARRIER_CODE. Join relationship: HMM_RDS_CARRIER_LATEST_V.CARRIER_CODE joins HMM_CARRIER_ASSIGNMENTS_V.CARRIER_CODE and HMM_RDS_CARRIERS_V.CARRIER_CODE.';

COMMENT ON COLUMN hmm_rds_carrier_latest_v.CARRIER_CODE IS
  'Primary Key. Join Key to HMM_CARRIER_ASSIGNMENTS_V(CARRIER_CODE) and HMM_RDS_CARRIERS_V(CARRIER_CODE).';

SELECT table_name, comments
FROM user_tab_comments
WHERE table_name IN (
  'HMM_CARRIER_ASSIGNMENTS_V',
  'HMM_RDS_CARRIERS_V',
  'HMM_RDS_CARRIER_PERF_V',
  'HMM_RDS_CARRIER_LATEST_V'
)
ORDER BY table_name;

SELECT table_name, column_name, comments
FROM user_col_comments
WHERE table_name IN (
  'HMM_CARRIER_ASSIGNMENTS_V',
  'HMM_RDS_CARRIERS_V',
  'HMM_RDS_CARRIER_PERF_V',
  'HMM_RDS_CARRIER_LATEST_V'
)
  AND column_name IN (
    'EMPLOYEE_CODE',
    'MANAGER_EMPLOYEE_CODE',
    'CARRIER_CODE',
    'PERFORMANCE_MONTH'
  )
ORDER BY table_name, column_name;
