CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    phone VARCHAR(15),
    email VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    salt VARCHAR(255) NOT NULL,
    drivers_id_checked BOOLEAN,
    role_id INT,
    payment_status VARCHAR(50),
    date_of_birth DATE,
    address VARCHAR(255),
    profile_picture VARCHAR(255),
    notes TEXT,
    status VARCHAR(50),
    FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE SET NULL
);

CREATE TABLE banking (
    id INTEGER PRIMARY KEY,
    user_id INT,
    iban VARCHAR(34) NOT NULL,
    bic VARCHAR(11) NOT NULL,
    mandate_reference VARCHAR(50),
    mandate_date_signed DATE,
    bank_name VARCHAR(100),
    account_holder_name VARCHAR(100),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE memberships (
    id INTEGER PRIMARY KEY,
    user_id INT NOT NULL,
    parent_id INT, 
    model_id INT,
    start_date DATE,
    end_date DATE,
    status VARCHAR(50),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE membership_plans (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    monthly_fee DECIMAL(10, 2),
    hourly_rate DECIMAL(10, 2),
    included_hours_per_year INT,
    remaining_hours_per_year INT,
    included_hours_per_month INT,
    remaining_hours_per_month INT,
    details TEXT
);

CREATE TABLE rentals (
    id INTEGER PRIMARY KEY,
    car_id INT,
    user_id INT,
    start_datetime DATETIME NOT NULL,
    end_datetime DATETIME NOT NULL,
    booking_datetime DATETIME NOT NULL,
    total_cost DECIMAL(10, 2),
    payment_method VARCHAR(50),
    status VARCHAR(50),
    FOREIGN KEY (car_id) REFERENCES cars(id),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE roles (
    id INTEGER PRIMARY KEY,
    role_name VARCHAR(50) NOT NULL
);

CREATE TABLE consents (
    id INTEGER PRIMARY KEY,
    user_id INT,
    updated_at DATE,
    created_at DATE,
    first_name VARCHAR(255),
    last_name VARCHAR(255),
    email VARCHAR(255),
    consent_type VARCHAR(255),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);

CREATE TABLE cars (
    id INTEGER PRIMARY KEY,
    licence_plate VARCHAR(15) NOT NULL UNIQUE,
    insurance VARCHAR(255) NOT NULL,
    acquired_date DATE NOT NULL,
    monthly_expenses DECIMAL(10, 2) NOT NULL,
    make VARCHAR(50),
    model VARCHAR(50),
    year INT,
    mileage INT,
    last_service_date DATE,
    status VARCHAR(50),
    vin VARCHAR(17) UNIQUE,
    fuel_type VARCHAR(20),
    gps BOOLEAN,
    current_location VARCHAR(255),
    service_history TEXT
);

CREATE TABLE email_verifications (
    id INTEGER PRIMARY KEY,
    user_id INT,
    verification_token VARCHAR(255) NOT NULL,
    created_at DATETIME NOT NULL,
    verified_at DATETIME DEFAULT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
