-- Your SQL goes here
CREATE TYPE user_role AS ENUM ('corp_admin', 'user');
CREATE TYPE replicant_status AS ENUM ('active', 'decommissioned');
CREATE TYPE replicant_gender AS ENUM ('male', 'female', 'non-binary');

CREATE TABLE IF NOT EXISTS corps (
    id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL UNIQUE,
    description TEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    invite_code VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE IF NOT EXISTS users (
    id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
    username VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role user_role NOT NULL DEFAULT 'user',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    corp_id UUID REFERENCES corps(id)
);

CREATE TABLE IF NOT EXISTS replicants (
    id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    status replicant_status NOT NULL DEFAULT 'active',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    gender replicant_gender NOT NULL,
    corp_id UUID REFERENCES corps(id) NOT NULL,
    is_private BOOLEAN NOT NULL DEFAULT TRUE,
    firmware_file VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS replicants_stats (
    replicant_id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid() REFERENCES replicants(id),
    health INTEGER NOT NULL DEFAULT 100,
    strength INTEGER NOT NULL DEFAULT 100,
    intelligence INTEGER NOT NULL DEFAULT 100,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_replicant_stats_replicant_id ON replicants_stats(replicant_id);
CREATE INDEX idx_replicants_status ON replicants(status);
