
CREATE SCHEMA IF NOT EXISTS auth;
CREATE SCHEMA IF NOT EXISTS workforce;

CREATE TABLE IF NOT EXISTS auth.login (
    LIKE public.login INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS auth.auth_remember_tokens (
    LIKE public.auth_remember_tokens INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS auth.login_attempt_tracker (
    LIKE public.login_attempt_tracker INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS auth.codigos_activacion (
    LIKE public.codigos_activacion INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS workforce.usuarios (
    LIKE public.usuarios INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS workforce.disponibilidad_usuarios (
    LIKE public.disponibilidad_usuarios INCLUDING ALL
);

CREATE TABLE IF NOT EXISTS workforce.excepciones_vacaciones (
    LIKE public.excepciones_vacaciones INCLUDING ALL
);

-- Reset canónico para reintentos de ejecución del migration sin dependencias rotas
TRUNCATE TABLE
    workforce.disponibilidad_usuarios,
    auth.auth_remember_tokens,
    auth.login,
    auth.login_attempt_tracker,
    auth.codigos_activacion,
    workforce.usuarios,
    workforce.excepciones_vacaciones
;

INSERT INTO auth.login
OVERRIDING SYSTEM VALUE
SELECT
    *
FROM public.login;

SELECT setval(
    pg_get_serial_sequence('auth.login', 'id_login'),
    COALESCE((SELECT MAX(id_login) FROM auth.login), 1),
    COALESCE((SELECT MAX(id_login) FROM auth.login), 0) > 0
);

INSERT INTO auth.auth_remember_tokens
SELECT
    *
FROM public.auth_remember_tokens;

CREATE SEQUENCE IF NOT EXISTS auth.auth_remember_tokens_id_seq
    AS bigint
    OWNED BY auth.auth_remember_tokens.id;

ALTER TABLE auth.auth_remember_tokens
    ALTER COLUMN id SET DEFAULT nextval('auth.auth_remember_tokens_id_seq'::regclass);

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

ALTER TABLE auth.auth_remember_tokens
    DROP CONSTRAINT IF EXISTS auth_remember_tokens_login_id_fkey;

ALTER TABLE auth.auth_remember_tokens
    ADD CONSTRAINT auth_remember_tokens_login_id_fkey
    FOREIGN KEY (login_id) REFERENCES auth.login (id_login)
    ON DELETE CASCADE;

INSERT INTO auth.login_attempt_tracker
SELECT
    *
FROM public.login_attempt_tracker;

INSERT INTO auth.codigos_activacion
SELECT
    *
FROM public.codigos_activacion;

CREATE SEQUENCE IF NOT EXISTS auth.codigos_activacion_id_seq
    AS integer
    OWNED BY auth.codigos_activacion.id;

ALTER TABLE auth.codigos_activacion
    ALTER COLUMN id SET DEFAULT nextval('auth.codigos_activacion_id_seq'::regclass);

SELECT setval(
    'auth.codigos_activacion_id_seq',
    COALESCE((SELECT MAX(id) FROM auth.codigos_activacion), 1),
    COALESCE((SELECT MAX(id) FROM auth.codigos_activacion), 0) > 0
);

INSERT INTO workforce.usuarios
OVERRIDING SYSTEM VALUE
SELECT
    *
FROM public.usuarios;

SELECT setval(
    pg_get_serial_sequence('workforce.usuarios', 'id_usuario'),
    COALESCE((SELECT MAX(id_usuario) FROM workforce.usuarios), 1),
    COALESCE((SELECT MAX(id_usuario) FROM workforce.usuarios), 0) > 0
);

INSERT INTO workforce.disponibilidad_usuarios
SELECT
    *
FROM public.disponibilidad_usuarios;

ALTER TABLE workforce.disponibilidad_usuarios
    DROP CONSTRAINT IF EXISTS fk_dispo_login;

ALTER TABLE workforce.disponibilidad_usuarios
    ADD CONSTRAINT fk_dispo_login
    FOREIGN KEY (id_login) REFERENCES auth.login (id_login)
    ON DELETE CASCADE;

INSERT INTO workforce.excepciones_vacaciones
SELECT
    *
FROM public.excepciones_vacaciones;

CREATE SEQUENCE IF NOT EXISTS workforce.excepciones_vacaciones_id_seq
    AS integer
    OWNED BY workforce.excepciones_vacaciones.id;

ALTER TABLE workforce.excepciones_vacaciones
    ALTER COLUMN id SET DEFAULT nextval('workforce.excepciones_vacaciones_id_seq'::regclass);

SELECT setval(
    'workforce.excepciones_vacaciones_id_seq',
    COALESCE((SELECT MAX(id) FROM workforce.excepciones_vacaciones), 1),
    COALESCE((SELECT MAX(id) FROM workforce.excepciones_vacaciones), 0) > 0
);

ANALYZE auth.login;
ANALYZE auth.auth_remember_tokens;
ANALYZE auth.login_attempt_tracker;
ANALYZE auth.codigos_activacion;
ANALYZE workforce.usuarios;
ANALYZE workforce.disponibilidad_usuarios;
ANALYZE workforce.excepciones_vacaciones;
