CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    first_name VARCHAR(255) NOT NULL,
    last_name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE  ,
    phone_number VARCHAR(20) UNIQUE,
    role VARCHAR(50) NOT NULL,
    password BYTEA NOT NULL,
    email_verified BOOLEAN NOT NULL DEFAULT FALSE,
    phone_verified BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMPTZ  NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ  ,
    --
    suspended_at TIMESTAMPTZ  NULL,  -- this can be NULL if the user is not suspended
    suspended BOOLEAN NOT NULL DEFAULT FALSE,
      CHECK (email IS NOT NULL OR phone_number IS NOT NULL) 
);
CREATE TABLE refresh_tokens (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    token TEXT NOT NULL UNIQUE,
    expires_at  TIMESTAMPTZ NOT NULL,
    created_at  TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
    revoked BOOLEAN DEFAULT FALSE NOT NULL,
    CONSTRAINT unique_token UNIQUE (token)
);
-----
   CREATE TABLE otps (
   id BIGSERIAL PRIMARY KEY,
    sent_to VARCHAR(255) NOT NULL,
    medium VARCHAR(50) NOT NULL, 
    otp_for VARCHAR(50) NOT NULL,    
    otp VARCHAR(10) NOT NULL,      
    used BOOLEAN NOT NULL DEFAULT FALSE,
    used_at TIMESTAMPTZ,         
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,  
    expires_at TIMESTAMPTZ NOT NULL 
);
----------------------------------------------seed data-------------------------------------------------------------
-------------------------------------- DO NOT USE IN PRODUCTION-------------------------------------------------

CREATE EXTENSION IF NOT EXISTS pgcrypto;

INSERT INTO users (
    first_name, last_name, email, phone_number, password, role, 
    email_verified, phone_verified, created_at, updated_at, 
    suspended_at, suspended
) VALUES (
    'John', 
    'Doe', 
    'john.doe@example.com', 
    NULL, 
    crypt('password123', gen_salt('bf'))::bytea,  
    'customer',  
    TRUE,      
    FALSE,       
    CURRENT_TIMESTAMP, 
    CURRENT_TIMESTAMP, 
    NULL,      
    FALSE       
);


CREATE TABLE IF NOT EXISTS bets (
	id BIGSERIAL PRIMARY KEY,
	amount BIGINT NOT NULL,
	total_odds REAL NOT NULL,
    status INT NOT NULL,
    full_name VARCHAR(255) NOT NULL,
    phone_number VARCHAR(255) NOT NULL,
    branch_id BIGINT,
    user_id BIGINT,
    cashed_out BOOLEAN DEFAULT FALSE,
	created_at TIMESTAMP,
    updated_at TIMESTAMP,
    CHECK (user_id IS NOT NULL OR branch_id IS NOT NULL)
);

CREATE TABLE IF NOT EXISTS tickets (
	id BIGSERIAL PRIMARY KEY,
	amount BIGINT NULL,
	total_odds REAL NOT NULL,
	created_at TIMESTAMP,
    updated_at TIMESTAMP  
);


-- CREATE TABLE IF NOT EXISTS bet_outcomes (
-- 	id BIGSERIAL PRIMARY KEY,
--     bet_id BIGINT NOT NULL,
-- 	outcome_id BIGINT NOT NULL,
-- );

-- CREATE TABLE IF NOT EXISTS ticket_outcomes (
-- 	id BIGSERIAL PRIMARY KEY,
--     ticket_id BIGINT NOT NULL,
-- 	outcome_id BIGINT NOT NULL,
-- );