-- ============================================================
-- Step 5: create local Oracle Views and Select AI metadata.
-- Explicit aliases normalize PostgreSQL lower-case identifiers
-- into ordinary Oracle identifiers.
-- Redmine: #730
-- ============================================================

CREATE OR REPLACE VIEW HMM_RDS_CARRIERS_V AS
SELECT
  "carrier_code"      AS carrier_code,
  "carrier_name"      AS carrier_name,
  "country_code"      AS country_code,
  "alliance_name"     AS alliance_name,
  "service_region"    AS service_region,
  "performance_tier" AS performance_tier
FROM "hmm_demo"."carriers"@HMM_RDS_PG_LINK;

COMMENT ON TABLE HMM_RDS_CARRIERS_V IS
  'HMM federation demo fictional carrier master. Primary Key: CARRIER_CODE. Referenced by HMM_RDS_CARRIER_PERF_V(CARRIER_CODE).';
COMMENT ON COLUMN HMM_RDS_CARRIERS_V.CARRIER_CODE IS
  'Primary Key. Fictional carrier identifier such as C001.';
COMMENT ON COLUMN HMM_RDS_CARRIERS_V.CARRIER_NAME IS
  'Fictional carrier display name; not an actual carrier.';
COMMENT ON COLUMN HMM_RDS_CARRIERS_V.COUNTRY_CODE IS
  'Two-letter demo country code.';
COMMENT ON COLUMN HMM_RDS_CARRIERS_V.ALLIANCE_NAME IS
  'Fictional carrier cooperation network.';
COMMENT ON COLUMN HMM_RDS_CARRIERS_V.SERVICE_REGION IS
  'Primary service region for the demo carrier.';
COMMENT ON COLUMN HMM_RDS_CARRIERS_V.PERFORMANCE_TIER IS
  'Demo portfolio tier: CORE, GROWTH, or WATCH.';

CREATE OR REPLACE VIEW HMM_RDS_CARRIER_PERF_V AS
SELECT
  "carrier_code"             AS carrier_code,
  "performance_month"        AS performance_month,
  "contracted_teu"           AS contracted_teu,
  "shipped_teu"              AS shipped_teu,
  "revenue_usd"              AS revenue_usd,
  "gross_margin_usd"         AS gross_margin_usd,
  "schedule_reliability_pct" AS schedule_reliability_pct,
  "vessel_utilization_pct"   AS vessel_utilization_pct,
  "claim_rate_pct"           AS claim_rate_pct,
  "yoy_growth_pct"           AS yoy_growth_pct,
  "risk_level"               AS risk_level
FROM "hmm_demo"."carrier_monthly_performance"@HMM_RDS_PG_LINK;

COMMENT ON TABLE HMM_RDS_CARRIER_PERF_V IS
  'HMM federation demo monthly carrier KPI. Composite Primary Key: CARRIER_CODE and PERFORMANCE_MONTH. Foreign Key: CARRIER_CODE references HMM_RDS_CARRIERS_V(CARRIER_CODE). All values are fictional.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.CARRIER_CODE IS
  'Foreign Key referencing HMM_RDS_CARRIERS_V(CARRIER_CODE).';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.PERFORMANCE_MONTH IS
  'First calendar day of the KPI month; part of the composite Primary Key.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.CONTRACTED_TEU IS
  'Contracted container volume in TEU.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.SHIPPED_TEU IS
  'Actually shipped container volume in TEU.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.REVENUE_USD IS
  'Fictional monthly revenue in USD.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.GROSS_MARGIN_USD IS
  'Fictional monthly gross margin in USD; negative values indicate a loss.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.SCHEDULE_RELIABILITY_PCT IS
  'Schedule reliability percentage from 0 through 100.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.VESSEL_UTILIZATION_PCT IS
  'Vessel capacity utilization percentage from 0 through 100.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.CLAIM_RATE_PCT IS
  'Cargo claim rate percentage from 0 through 100.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.YOY_GROWTH_PCT IS
  'Year-over-year performance growth percentage.';
COMMENT ON COLUMN HMM_RDS_CARRIER_PERF_V.RISK_LEVEL IS
  'Derived demo risk category: GREEN, AMBER, or RED.';

CREATE OR REPLACE VIEW HMM_RDS_CARRIER_LATEST_V AS
SELECT
  "carrier_code"             AS carrier_code,
  "carrier_name"             AS carrier_name,
  "country_code"             AS country_code,
  "alliance_name"            AS alliance_name,
  "service_region"           AS service_region,
  "performance_tier"        AS performance_tier,
  "performance_month"        AS performance_month,
  "contracted_teu"           AS contracted_teu,
  "shipped_teu"              AS shipped_teu,
  "revenue_usd"              AS revenue_usd,
  "gross_margin_usd"         AS gross_margin_usd,
  "schedule_reliability_pct" AS schedule_reliability_pct,
  "vessel_utilization_pct"   AS vessel_utilization_pct,
  "claim_rate_pct"           AS claim_rate_pct,
  "yoy_growth_pct"           AS yoy_growth_pct,
  "risk_level"               AS risk_level
FROM "hmm_demo"."carrier_performance_latest_v"@HMM_RDS_PG_LINK;

COMMENT ON TABLE HMM_RDS_CARRIER_LATEST_V IS
  'Latest fictional monthly KPI for each HMM federation demo carrier. Primary Key: CARRIER_CODE.';
COMMENT ON COLUMN HMM_RDS_CARRIER_LATEST_V.CARRIER_CODE IS
  'Primary Key for the latest carrier KPI row.';
COMMENT ON COLUMN HMM_RDS_CARRIER_LATEST_V.PERFORMANCE_MONTH IS
  'Latest KPI month available in the PostgreSQL source.';
COMMENT ON COLUMN HMM_RDS_CARRIER_LATEST_V.RISK_LEVEL IS
  'Latest derived risk category: GREEN, AMBER, or RED.';

SELECT object_name, object_type, status
FROM user_objects
WHERE object_name IN (
  'HMM_RDS_CARRIERS_V',
  'HMM_RDS_CARRIER_PERF_V',
  'HMM_RDS_CARRIER_LATEST_V'
)
ORDER BY object_name;
