-- 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'))
);

-- 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')),
    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,
    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
);