BEGIN;

SET LOCAL lock_timeout = '10s';
SET LOCAL statement_timeout = '900000';
SET LOCAL idle_in_transaction_session_timeout = '900000';

CREATE SEQUENCE IF NOT EXISTS tmp_fk_test.entity_category_links_id_link_seq;
SELECT setval(
    'tmp_fk_test.entity_category_links_id_link_seq',
    GREATEST(COALESCE((SELECT MAX(id_link) FROM tmp_fk_test.entity_category_links), 0), 1),
    COALESCE((SELECT MAX(id_link) FROM tmp_fk_test.entity_category_links), 0) > 0
);
ALTER TABLE tmp_fk_test.entity_category_links
    ALTER COLUMN id_link SET DEFAULT nextval('tmp_fk_test.entity_category_links_id_link_seq'::regclass);
ALTER SEQUENCE tmp_fk_test.entity_category_links_id_link_seq
    OWNED BY tmp_fk_test.entity_category_links.id_link;

DROP VIEW IF EXISTS public.error_log;
DROP VIEW IF EXISTS public.pos_idempotency_requests;
DROP VIEW IF EXISTS public.webhook_ingress_dlq;
DROP VIEW IF EXISTS public.webhook_ingress_events;
DROP VIEW IF EXISTS public.categorias_combinables_v2;
DROP VIEW IF EXISTS public.category_rules_v2;
DROP VIEW IF EXISTS public.entity_category_links;
DROP VIEW IF EXISTS public.inventario;
DROP VIEW IF EXISTS public.inventario_variantes_categoria;
DROP VIEW IF EXISTS public.inventario_variantes_resolucion_log;

DROP TRIGGER IF EXISTS trg_cascade_soft_delete_rules_v2 ON public_legacy.categorias_combinables_v2;
DROP TRIGGER IF EXISTS trg_cascade_soft_delete_variantes_v2 ON public_legacy.categorias_combinables_v2;
DROP TRIGGER IF EXISTS trg_cat_v2_updated_at ON public_legacy.categorias_combinables_v2;
DROP TRIGGER IF EXISTS trg_clean_preguntas_on_category_deactivate ON public_legacy.categorias_combinables_v2;
DROP TRIGGER IF EXISTS trg_clean_v2_category_from_jsonb ON public_legacy.categorias_combinables_v2;
DROP TRIGGER IF EXISTS trg_rules_v2_updated_at ON public_legacy.category_rules_v2;
DROP TRIGGER IF EXISTS trg_validate_category_rule_v2 ON public_legacy.category_rules_v2;
DROP TRIGGER IF EXISTS trg_ivc_updated_at ON public_legacy.inventario_variantes_categoria;

DROP TABLE IF EXISTS public_legacy.error_log;
DROP TABLE IF EXISTS public_legacy.pos_idempotency_requests;
DROP TABLE IF EXISTS public_legacy.webhook_ingress_dlq;
DROP TABLE IF EXISTS public_legacy.webhook_ingress_events;
DROP TABLE IF EXISTS public_legacy.entity_category_links;
DROP TABLE IF EXISTS public_legacy.inventario_variantes_categoria;
DROP TABLE IF EXISTS public_legacy.inventario_variantes_resolucion_log;
DROP TABLE IF EXISTS public_legacy.category_rules_v2;
DROP TABLE IF EXISTS public_legacy.inventario;
DROP TABLE IF EXISTS public_legacy.categorias_combinables_v2;

DROP SEQUENCE IF EXISTS public_legacy.error_log_id_error_seq;
DROP SEQUENCE IF EXISTS public_legacy.pos_idempotency_requests_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.webhook_ingress_dlq_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.webhook_ingress_events_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.categorias_combinables_v2_category_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.category_rules_v2_rule_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.entity_category_links_id_link_seq;
DROP SEQUENCE IF EXISTS public_legacy.inventario_id_item_seq;
DROP SEQUENCE IF EXISTS public_legacy.inventario_variantes_categoria_id_variante_seq;
DROP SEQUENCE IF EXISTS public_legacy.inventario_variantes_resolucion_log_id_log_seq;

DROP FUNCTION IF EXISTS public.categorias_inherit_sucursal();
DROP FUNCTION IF EXISTS public.categorias_set_path();
DROP FUNCTION IF EXISTS public.enforce_item_categoria_sucursal();
DROP FUNCTION IF EXISTS public.fn_cascade_soft_delete_rules_v2();
DROP FUNCTION IF EXISTS public.fn_cascade_soft_delete_variantes_on_category_delete();
DROP FUNCTION IF EXISTS public.fn_cat_v2_updated_at();
DROP FUNCTION IF EXISTS public.fn_clean_preguntas_on_category_deactivate();
DROP FUNCTION IF EXISTS public.fn_clean_v2_category_from_jsonb();
DROP FUNCTION IF EXISTS public.fn_ivc_updated_at();
DROP FUNCTION IF EXISTS public.fn_rules_v2_updated_at();
DROP FUNCTION IF EXISTS public.fn_validate_category_rule_v2();
DROP FUNCTION IF EXISTS public.jsonb_to_intarray(jsonb);
DROP FUNCTION IF EXISTS public.trg_itemcategoria_rules();

COMMIT;
