CREATE TABLE users (
    id INT 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),
    membership_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 INT 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 membership (
    id INT PRIMARY KEY,
    user_id INT,
    membership_fee DECIMAL(10, 2),
    rental_fee DECIMAL(10, 2),
    included_hours_per_year INT,
    remaining_hours_per_year INT,
    included_hours_per_month INT,
    remaining_hours_per_month INT,
    membership_model VARCHAR(50),
    membership_id VARCHAR(50),
    membership_start_date DATE,
    membership_end_date DATE,
    discount_rate DECIMAL(5, 2),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE rentals (
    id INT 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 INT PRIMARY KEY,
    role_name VARCHAR(50) NOT NULL
);

CREATE TABLE consents (
    id INT PRIMARY KEY,
    user_id INT,
    modified DATE,
    created DATE,
    first_name VARCHAR(255),
    last_name VARCHAR(255),
    email VARCHAR(255),
    type VARCHAR(255),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);

CREATE TABLE cars (
    id INT 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 INT PRIMARY KEY,
    user_id INT,
    verification_token VARCHAR(255) NOT NULL,
    created_at DATETIME NOT NULL,
    verified_at DATETIME,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
