CREATE TABLE IF NOT EXISTS users (
    id BIGSERIAL PRIMARY KEY,
    email citext UNIQUE NOT NULL,
    name TEXT NOT NULL,
    alt_name TEXT,
    created TIMESTAMPTZ NOT NULL DEFAULT now(),
    last_login TIMESTAMPTZ NOT NULL DEFAULT '1970-01-01 00:00:00+00',
    activated BOOL NOT NULL DEFAULT false,
    version INT NOT NULL DEFAULT 1
);

CREATE TABLE IF NOT EXISTS auth_provider (
    id BIGSERIAL PRIMARY KEY, -- e.g., 'local', 'id_austria',
    description TEXT NOT NULL,
    active BOOLEAN DEFAULT false
);

INSERT INTO auth_provider (description, active) VALUES ('local', true);
INSERT INTO auth_provider (description, active) VALUES ('id_austria', false);

CREATE TABLE IF NOT EXISTS user_identities (
    id BIGSERIAL PRIMARY KEY,
    provider_id BIGINT NOT NULL REFERENCES auth_provider(id),
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    
    -- For local: the username. For OIDC: the 'sub' (Subject ID)
    provider_user_id TEXT NOT NULL,
    
    -- Nullable because OIDC users won't have a password in your DB
    password bytea,

    version INT NOT NULL DEFAULT 1,
    
    UNIQUE(provider_id, provider_user_id)
);

-- INSERT INTO users(
--     username,
--     email,
--     password,
--     name,
--     alt_name
-- )
-- VALUES(
--     'vik',
--     'vikhenzo@gmail.com',
--     '$argon2id$v=19$m=65536,t=1,p=32$+dQ9uB7kKL7t7G3bI+TOMw$Wvic27W6SYH6Fx2Pp84irhVJ/blVh5qINlkv58bpgEc',
--     'Vicente Ferrari Smith',
--     'Vicente'
-- );

-- INSERT INTO users(
--     username,
--     email,
--     password,
--     name,
--     alt_name
-- )
-- VALUES(
--     'mkBflwkpe',
--     'bellrebekaou@gmail.com',
--     '$argon2id$v=19$m=65536,t=1,p=1$BWcWdp8bhgS84LWqUCb2IA$DGF/FzQbSNHnfZraE9F2qvfdBGf5XB81+w00QgY/jG0',
--     'zWkxKTNolTgJwO',
--     'OahedOBLSo'
-- );
