BEGIN;

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

INSERT INTO auth.auth_remember_tokens (
    id,
    login_id,
    device_id,
    token_id,
    token_hash,
    user_agent_hash,
    created_at,
    last_used_at,
    expires_at,
    revoked_at
)
SELECT
    l.id,
    l.login_id,
    l.device_id,
    l.token_id,
    l.token_hash,
    l.user_agent_hash,
    l.created_at,
    l.last_used_at,
    l.expires_at,
    l.revoked_at
FROM public_legacy.auth_remember_tokens l
LEFT JOIN auth.auth_remember_tokens c
  ON c.id = l.id
WHERE c.id IS NULL;

SELECT setval(
    'auth.auth_remember_tokens_id_seq',
    COALESCE((SELECT MAX(id) FROM auth.auth_remember_tokens), 1),
    EXISTS (SELECT 1 FROM auth.auth_remember_tokens)
);

DROP VIEW IF EXISTS public.auth_remember_tokens;
DROP VIEW IF EXISTS public.codigos_activacion;
DROP VIEW IF EXISTS public.login;
DROP VIEW IF EXISTS public.login_attempt_tracker;
DROP VIEW IF EXISTS public.disponibilidad_usuarios;
DROP VIEW IF EXISTS public.excepciones_vacaciones;
DROP VIEW IF EXISTS public.usuarios;
DROP VIEW IF EXISTS public.numeros_whatsapp_business;
DROP VIEW IF EXISTS public.whatsapp_templates;
DROP VIEW IF EXISTS public.whatsapp_templates_for_staff;
DROP VIEW IF EXISTS public.citas;
DROP VIEW IF EXISTS public.comprobantes_transacciones;
DROP VIEW IF EXISTS public.configuraciones_planes_pago;
DROP VIEW IF EXISTS public.modelos_planes_pago;
DROP VIEW IF EXISTS public.pagos_planes;
DROP VIEW IF EXISTS public.planes_pago;
DROP VIEW IF EXISTS public.transacciones;
DROP VIEW IF EXISTS public.ventas;

DROP TRIGGER IF EXISTS trg_refresca_disp_citas ON public_legacy.citas;
DROP TRIGGER IF EXISTS trg_sync_availability_user ON public_legacy.citas;
DROP TRIGGER IF EXISTS trg_refresca_disp_exc ON public_legacy.excepciones_vacaciones;
DROP TRIGGER IF EXISTS trg_refresca_trabajo_exc ON public_legacy.excepciones_vacaciones;
DROP TRIGGER IF EXISTS trg_sync_excepcion_user ON public_legacy.excepciones_vacaciones;
DROP TRIGGER IF EXISTS trg_sync_login_dispo ON public_legacy.login;
DROP TRIGGER IF EXISTS trg_modelos_pp_updated_at ON public_legacy.modelos_planes_pago;
DROP TRIGGER IF EXISTS trg_planes_pago_updated_at ON public_legacy.planes_pago;
DROP TRIGGER IF EXISTS trg_refresca_jornadas_usuarios ON public_legacy.usuarios;
DROP TRIGGER IF EXISTS trg_sync_autorizaciones ON public_legacy.usuarios;
DROP TRIGGER IF EXISTS trg_ventas_updated_at ON public_legacy.ventas;

ALTER TABLE IF EXISTS public_legacy.auth_remember_tokens
    DROP CONSTRAINT IF EXISTS auth_remember_tokens_login_id_fkey;
ALTER TABLE IF EXISTS public_legacy.disponibilidad_usuarios
    DROP CONSTRAINT IF EXISTS fk_dispo_login;
ALTER TABLE IF EXISTS public_legacy.comprobantes_transacciones
    DROP CONSTRAINT IF EXISTS fk_comprobante_transaccion;
ALTER TABLE IF EXISTS public_legacy.comprobantes_transacciones
    DROP CONSTRAINT IF EXISTS fk_comprobantes_venta;
ALTER TABLE IF EXISTS public_legacy.pagos_planes
    DROP CONSTRAINT IF EXISTS fk_pago_pagador;
ALTER TABLE IF EXISTS public_legacy.pagos_planes
    DROP CONSTRAINT IF EXISTS fk_pago_plan;
ALTER TABLE IF EXISTS public_legacy.planes_pago
    DROP CONSTRAINT IF EXISTS fk_planes_cliente;
ALTER TABLE IF EXISTS public_legacy.planes_pago
    DROP CONSTRAINT IF EXISTS fk_planes_pago_transaccion;
ALTER TABLE IF EXISTS public_legacy.planes_pago
    DROP CONSTRAINT IF EXISTS fk_planes_pago_venta;
ALTER TABLE IF EXISTS public_legacy.transacciones
    DROP CONSTRAINT IF EXISTS fk_transacciones_plan_pago;
ALTER TABLE IF EXISTS public_legacy.transacciones
    DROP CONSTRAINT IF EXISTS fk_transacciones_venta;
ALTER TABLE IF EXISTS public_legacy.ventas
    DROP CONSTRAINT IF EXISTS fk_ventas_cliente;
ALTER TABLE IF EXISTS public_legacy.ventas
    DROP CONSTRAINT IF EXISTS fk_ventas_plan_pago;


DROP SEQUENCE IF EXISTS public.backend_agent_test_id_test_seq;
DROP SEQUENCE IF EXISTS public_legacy.auth_remember_tokens_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.codigos_activacion_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.citas_id_cita_seq;
DROP SEQUENCE IF EXISTS public_legacy.comprobantes_transacciones_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.configuraciones_planes_pago_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.excepciones_vacaciones_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.modelos_planes_pago_id_modelo_seq;
DROP SEQUENCE IF EXISTS public_legacy.numeros_whatsapp_business_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.pagos_planes_id_pago_seq;
DROP SEQUENCE IF EXISTS public_legacy.planes_pago_id_plan_seq;
DROP SEQUENCE IF EXISTS public_legacy.transacciones_id_transaccion_seq;
DROP SEQUENCE IF EXISTS public_legacy.usuarios_id_usuario_seq;
DROP SEQUENCE IF EXISTS public_legacy.ventas_id_venta_seq;
DROP SEQUENCE IF EXISTS public_legacy.whatsapp_templates_id_seq;
DROP SEQUENCE IF EXISTS public_legacy.whatsapp_templates_for_staff_id_seq;

DROP FUNCTION IF EXISTS public.apply_block_user(integer, tstzrange, integer);
DROP FUNCTION IF EXISTS public.apply_block_user(integer, tstzrange, text, boolean);
DROP FUNCTION IF EXISTS public.fill_jornada_sucursal_from_login(integer);
DROP FUNCTION IF EXISTS public.fn_remover_sucursal_global(character varying, integer);
DROP FUNCTION IF EXISTS public.hhmm_to_time(text);
DROP FUNCTION IF EXISTS public.jornada_json_to_ranges(jsonb, date, interval);
DROP FUNCTION IF EXISTS public.manual_sync_disponibilidad();
DROP FUNCTION IF EXISTS public.verificar_disponibilidad_periodos(character varying);
DROP FUNCTION IF EXISTS public.verificar_disponibilidad_periodos(character varying, timestamp with time zone, timestamp with time zone);
DROP FUNCTION IF EXISTS public.refresca_jornadas(integer);
DROP FUNCTION IF EXISTS public.refresca_jornadas_sucursal(integer);
DROP FUNCTION IF EXISTS public.refresca_jornadas_trigger();
DROP FUNCTION IF EXISTS public.set_updated_at_timestamp();
DROP FUNCTION IF EXISTS public.sync_availability_user();
DROP FUNCTION IF EXISTS public.sync_excepcion_user();
DROP FUNCTION IF EXISTS public.sync_login_dispo();
DROP FUNCTION IF EXISTS public.trigger_set_updated_at();
DROP FUNCTION IF EXISTS public.trig_refresca_disp_disponible();
DROP FUNCTION IF EXISTS public.trig_refresca_disp_exc();
DROP FUNCTION IF EXISTS public.trig_refresca_disp_trabajo();
DROP FUNCTION IF EXISTS public.trig_refresca_jornadas_config();
DROP FUNCTION IF EXISTS public.trig_refresca_jornadas_usuarios();
DROP FUNCTION IF EXISTS public.trig_refresca_trabajo_exc();
DROP FUNCTION IF EXISTS public.trig_sync_autorizaciones();

COMMIT;
