-- Reset schema: drop tables first (children → parents), then recreate.

BEGIN TRANSACTION;
PRAGMA foreign_keys=OFF;

-- Drop child tables first to avoid FK constraints
DROP TABLE IF EXISTS order_in_points;
DROP TABLE IF EXISTS order_out_points;
DROP TABLE IF EXISTS orders_in;
DROP TABLE IF EXISTS orders_out;
DROP TABLE IF EXISTS email;
DROP TABLE IF EXISTS subscriptions;
DROP TABLE IF EXISTS company_user_access;
DROP TABLE IF EXISTS destinations;
DROP TABLE IF EXISTS transporters;
DROP TABLE IF EXISTS clients;
DROP TABLE IF EXISTS currency;
DROP TABLE IF EXISTS users;

PRAGMA foreign_keys=ON;
COMMIT;

-- Users table

CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    contact_name TEXT,
    email TEXT UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    phone TEXT,
    register_number TEXT,
    vat TEXT,
    address TEXT,
    logo_filename TEXT,
    terms TEXT,
    first_order_number INTEGER DEFAULT 1,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    otp_code TEXT,
    otp_expiration TIMESTAMPTZ,
    user_role TEXT NOT NULL DEFAULT 'user' CHECK (user_role IN ('user', 'admin', 'company_user')),
    company_id INTEGER DEFAULT 0, 
    active INTEGER DEFAULT 1,
    temporary_password INTEGER DEFAULT 0
);

-- Clients table
CREATE TABLE IF NOT EXISTS clients (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    name TEXT NOT NULL,
    address TEXT,
    register_number TEXT,
    contact_person TEXT,
    phone TEXT,
    email TEXT,
    vat TEXT,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Transporters table
CREATE TABLE IF NOT EXISTS transporters (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    name TEXT NOT NULL,
    address TEXT,
    register_number TEXT,
    contact_person TEXT,
    phone TEXT,
    email TEXT,
    vat TEXT,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Destinations table
CREATE TABLE IF NOT EXISTS destinations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    name TEXT NOT NULL,
    address TEXT,
    latitude TEXT,
    longitude TEXT,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Orders out table
CREATE TABLE IF NOT EXISTS orders_out (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    order_number TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    client_id INTEGER NOT NULL,
    transporter_id INTEGER NOT NULL,
    products_description TEXT,
    ldb_quantity DOUBLE PRECISION,
    kg_quantity DOUBLE PRECISION,
    track_reg_number TEXT,
    trailer_reg_number TEXT,
    received_price DOUBLE PRECISION,
    paid_price DOUBLE PRECISION,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    order_status TEXT NOT NULL DEFAULT 'active' CHECK (order_status IN ('active', 'inactive', 'cancelled')),
    order_in_number TEXT,
    currency_received TEXT,
    currency_paid TEXT,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY(client_id) REFERENCES clients(id) ON DELETE CASCADE,
    FOREIGN KEY(transporter_id) REFERENCES transporters(id) ON DELETE CASCADE
);

-- Orders in table
CREATE TABLE IF NOT EXISTS orders_in (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    order_number TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    client_id INTEGER NOT NULL,
    products_description TEXT,
    ldb_quantity DOUBLE PRECISION,
    kg_quantity DOUBLE PRECISION,
    track_reg_number TEXT,
    trailer_reg_number TEXT,
    received_price DOUBLE PRECISION,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    file_name TEXT,
    expenses DOUBLE PRECISION,
    currency_received TEXT,
    currency_expenses TEXT,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY(client_id) REFERENCES clients(id) ON DELETE CASCADE
);

-- Order In Points (loading/unloading) table
CREATE TABLE IF NOT EXISTS order_in_points (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    order_id INTEGER NOT NULL,
    destination_id INTEGER NOT NULL,
    informatins TEXT,
    point_data TEXT,
    point_hour TEXT,
    point_type TEXT NOT NULL CHECK (point_type IN ('loading', 'unloading')),
    FOREIGN KEY(order_id) REFERENCES orders_in(id) ON DELETE CASCADE
);

-- Order In Points (loading/unloading) table
CREATE TABLE IF NOT EXISTS order_out_points (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    order_id INTEGER NOT NULL,
    destination_id INTEGER NOT NULL,
    informatins TEXT,
    point_data TEXT,
    point_hour TEXT,
    point_type TEXT NOT NULL CHECK (point_type IN ('loading', 'unloading')),
    FOREIGN KEY(order_id) REFERENCES orders_out(id) ON DELETE CASCADE
);

-- Subscriptions table
CREATE TABLE IF NOT EXISTS subscriptions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    plan TEXT NOT NULL CHECK (plan IN ('first_2_months', 'monthly', 'yearly')),
    start_date TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    end_date TIMESTAMPTZ NOT NULL,
    status TEXT NOT NULL CHECK (status IN ('active', 'expired', 'cancelled')),
    register_number TEXT,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS email (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    smtp_host TEXT,
    smtp_port TEXT, 
    smtp_user TEXT,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS company_user_access (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    company_user_id INTEGER NOT NULL,
    clients INTEGER,
    transporters INTEGER, 
    destinations INTEGER,
    orders_in INTEGER,
    orders_out INTEGER,
    report INTEGER,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(company_user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS currency (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    name TEXT,
    value DOUBLE PRECISION, 
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);