BEGIN;

CREATE SCHEMA IF NOT EXISTS mv_edge_oee_ucepsa_prod;
CREATE SCHEMA IF NOT EXISTS mv_reports_ucepsa_prod;

-- Cache comercial/productiva de Odoo PROD.
-- Se clona estructura desde la tabla demo existente, pero se corrige la secuencia del id.
CREATE TABLE IF NOT EXISTS mv_edge_oee_ucepsa_prod.odoo_order_commercials
(
    LIKE mv_edge_oee_ucepsa_demo.odoo_order_commercials
    INCLUDING ALL
);

CREATE SEQUENCE IF NOT EXISTS mv_edge_oee_ucepsa_prod.odoo_order_commercials_id_seq
    OWNED BY mv_edge_oee_ucepsa_prod.odoo_order_commercials.id;

ALTER TABLE mv_edge_oee_ucepsa_prod.odoo_order_commercials
    ALTER COLUMN id SET DEFAULT nextval('mv_edge_oee_ucepsa_prod.odoo_order_commercials_id_seq'::regclass);

-- Parámetros económicos productivos.
CREATE TABLE IF NOT EXISTS mv_edge_oee_ucepsa_prod.margin_cost_parameters
(
    LIKE mv_edge_oee_ucepsa_demo.margin_cost_parameters
    INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS mv_edge_oee_ucepsa_prod.raw_material_cost_parameters
(
    LIKE mv_edge_oee_ucepsa_demo.raw_material_cost_parameters
    INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS mv_edge_oee_ucepsa_prod.data_quality_contract_rules
(
    LIKE mv_edge_oee_ucepsa_demo.data_quality_contract_rules
    INCLUDING ALL
);

-- Copia de reglas DQC demo a contexto productivo.
-- Mantiene la lógica, cambia site a ucepsa_onpremise.
INSERT INTO mv_edge_oee_ucepsa_prod.data_quality_contract_rules (
    tenant,
    site,
    rule_code,
    rule_name,
    domain,
    dimension,
    severity,
    owner_area,
    applies_to,
    description,
    expected_condition,
    blocking_for,
    is_active,
    created_at
)
SELECT
    tenant,
    'ucepsa_onpremise' AS site,
    rule_code,
    rule_name,
    domain,
    dimension,
    severity,
    owner_area,
    applies_to,
    description,
    expected_condition,
    blocking_for,
    is_active,
    now()
FROM mv_edge_oee_ucepsa_demo.data_quality_contract_rules d
WHERE d.tenant = 'ucepsa'
  AND d.site = 'ucepsa_test'
  AND NOT EXISTS (
      SELECT 1
      FROM mv_edge_oee_ucepsa_prod.data_quality_contract_rules p
      WHERE p.tenant = d.tenant
        AND p.site = 'ucepsa_onpremise'
        AND p.rule_code = d.rule_code
  );

-- Permisos defensivos si los roles existen.
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'grafana_ucepsa_ro') THEN
        GRANT USAGE ON SCHEMA mv_edge_oee_ucepsa_prod TO grafana_ucepsa_ro;
        GRANT USAGE ON SCHEMA mv_reports_ucepsa_prod TO grafana_ucepsa_ro;
        GRANT SELECT ON ALL TABLES IN SCHEMA mv_edge_oee_ucepsa_prod TO grafana_ucepsa_ro;
        GRANT SELECT ON ALL TABLES IN SCHEMA mv_reports_ucepsa_prod TO grafana_ucepsa_ro;
    END IF;

    IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'edge_oee_odoo_sync_writer') THEN
        GRANT USAGE ON SCHEMA mv_edge_oee_ucepsa_prod TO edge_oee_odoo_sync_writer;
        GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA mv_edge_oee_ucepsa_prod TO edge_oee_odoo_sync_writer;
        GRANT USAGE, SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA mv_edge_oee_ucepsa_prod TO edge_oee_odoo_sync_writer;
    END IF;
END $$;

COMMIT;
