
CREATE SCHEMA IF NOT EXISTS messaging;

CREATE TABLE IF NOT EXISTS messaging.numeros_whatsapp_business (
    LIKE public.numeros_whatsapp_business INCLUDING ALL
);

TRUNCATE TABLE messaging.numeros_whatsapp_business;

INSERT INTO messaging.numeros_whatsapp_business (
    "id", "phone_number", "business_id_display", "sucursal_id_legacy", "branch_priority",
    "business_id", "sucursal", "soft_delete", "whatsapp_contact_url", "meta_access_token_encrypted",
    "meta_phone_number_id", "meta_waba_id", "status", "pin_2fa_encrypted", "display_name",
    "display_name_status", "code_verification_status", "webhook_subscribed", "registered_at",
    "signup_started_at", "session_info_received_at", "registration_attempts",
    "last_registration_attempt_at", "last_error_code", "last_error_message", "last_error_at",
    "webhook_subscribed_at", "last_webhook_event", "last_webhook_at", "api_call_count",
    "last_api_success_at"
)
SELECT
    "id", "phone_number", "business_id_display", "sucursal_id_legacy", "branch_priority",
    "business_id", "sucursal", "soft_delete", "whatsapp_contact_url", "meta_access_token_encrypted",
    "meta_phone_number_id", "meta_waba_id", "status", "pin_2fa_encrypted", "display_name",
    "display_name_status", "code_verification_status", "webhook_subscribed", "registered_at",
    "signup_started_at", "session_info_received_at", "registration_attempts",
    "last_registration_attempt_at", "last_error_code", "last_error_message", "last_error_at",
    "webhook_subscribed_at", "last_webhook_event", "last_webhook_at", "api_call_count",
    "last_api_success_at"
FROM public.numeros_whatsapp_business;

CREATE SEQUENCE IF NOT EXISTS messaging.numeros_whatsapp_business_id_seq
    AS integer
    OWNED BY messaging.numeros_whatsapp_business.id;

ALTER TABLE messaging.numeros_whatsapp_business
    ALTER COLUMN id SET DEFAULT nextval('messaging.numeros_whatsapp_business_id_seq'::regclass);

SELECT setval(
    'messaging.numeros_whatsapp_business_id_seq',
    COALESCE((SELECT MAX(id) FROM messaging.numeros_whatsapp_business), 1),
    COALESCE((SELECT MAX(id) FROM messaging.numeros_whatsapp_business), 0) > 0
);

CREATE TABLE IF NOT EXISTS messaging.whatsapp_templates (
    LIKE public.whatsapp_templates INCLUDING ALL
);

TRUNCATE TABLE messaging.whatsapp_templates;

INSERT INTO messaging.whatsapp_templates (
    "id", "business_id", "sucursal_id", "phone_number_id", "template_name", "language",
    "body_text", "components", "waba_id", "access_token_encrypted", "token_updated_at",
    "variable_mapping", "description", "display_name", "template_type"
)
SELECT
    "id", "business_id", "sucursal_id", "phone_number_id", "template_name", "language",
    "body_text", "components", "waba_id", "access_token_encrypted", "token_updated_at",
    "variable_mapping", "description", "display_name", "template_type"
FROM public.whatsapp_templates;

CREATE SEQUENCE IF NOT EXISTS messaging.whatsapp_templates_id_seq
    AS integer
    OWNED BY messaging.whatsapp_templates.id;

ALTER TABLE messaging.whatsapp_templates
    ALTER COLUMN id SET DEFAULT nextval('messaging.whatsapp_templates_id_seq'::regclass);

SELECT setval(
    'messaging.whatsapp_templates_id_seq',
    COALESCE((SELECT MAX(id) FROM messaging.whatsapp_templates), 1),
    COALESCE((SELECT MAX(id) FROM messaging.whatsapp_templates), 0) > 0
);

CREATE TABLE IF NOT EXISTS messaging.whatsapp_templates_for_staff (
    LIKE public.whatsapp_templates_for_staff INCLUDING ALL
);

TRUNCATE TABLE messaging.whatsapp_templates_for_staff;

INSERT INTO messaging.whatsapp_templates_for_staff (
    "id", "phone_number_id", "template_name", "language", "body_text", "waba_id",
    "access_token_encrypted", "token_updated_at", "variable_mapping", "description", "display_name",
    "template_type", "components"
)
SELECT
    "id", "phone_number_id", "template_name", "language", "body_text", "waba_id",
    "access_token_encrypted", "token_updated_at", "variable_mapping", "description", "display_name",
    "template_type", "components"
FROM public.whatsapp_templates_for_staff;

CREATE SEQUENCE IF NOT EXISTS messaging.whatsapp_templates_for_staff_id_seq
    AS integer
    OWNED BY messaging.whatsapp_templates_for_staff.id;

ALTER TABLE messaging.whatsapp_templates_for_staff
    ALTER COLUMN id SET DEFAULT nextval('messaging.whatsapp_templates_for_staff_id_seq'::regclass);

SELECT setval(
    'messaging.whatsapp_templates_for_staff_id_seq',
    COALESCE((SELECT MAX(id) FROM messaging.whatsapp_templates_for_staff), 1),
    COALESCE((SELECT MAX(id) FROM messaging.whatsapp_templates_for_staff), 0) > 0
);

ANALYZE messaging.numeros_whatsapp_business;
ANALYZE messaging.whatsapp_templates;
ANALYZE messaging.whatsapp_templates_for_staff;
