create database if not exists car_rental default character set utf8mb4 collate utf8mb4_general_ci;
use car_rental;

create table if not exists users (
    id bigint primary key auto_increment,
    username varchar(50) not null unique,
    password varchar(255) not null,
    phone varchar(50),
    email varchar(100),
    role varchar(20) not null,
    status varchar(20) not null,
    balance decimal(10,2) default 0,
    real_name_status varchar(20) default 'NONE',
    real_name varchar(50),
    id_number varchar(30),
    id_front varchar(255),
    id_back varchar(255),
    created_at datetime,
    updated_at datetime
);

create table if not exists cars (
    id bigint primary key auto_increment,
    brand varchar(50),
    model varchar(50),
    plate_no varchar(50),
    price_per_day decimal(10,2),
    deposit decimal(10,2),
    status varchar(20),
    is_special tinyint(1) default 0,
    images text,
    description text,
    seats int,
    transmission varchar(20),
    fuel_type varchar(20),
    mileage int,
    created_at datetime,
    updated_at datetime
);

create table if not exists favorites (
    id bigint primary key auto_increment,
    user_id bigint not null,
    car_id bigint not null,
    created_at datetime,
    unique key uk_user_car(user_id, car_id)
);

create table if not exists orders (
    id bigint primary key auto_increment,
    order_no varchar(64) not null unique,
    user_id bigint not null,
    car_id bigint not null,
    start_date date,
    end_date date,
    days int,
    price_per_day decimal(10,2),
    deposit decimal(10,2),
    total_amount decimal(10,2),
    status varchar(20),
    pay_type varchar(20),
    paid_at datetime,
    created_at datetime,
    updated_at datetime
);

create table if not exists payments (
    id bigint primary key auto_increment,
    order_id bigint not null,
    user_id bigint not null,
    amount decimal(10,2),
    type varchar(20),
    created_at datetime
);

insert into users(username, password, phone, email, role, status, balance, real_name_status, created_at, updated_at)
values ('admin', '$2a$10$7hErhcmS8xj6QcGbe0yE0eDBn5OQWw4tGxqVylxYxe3CxbJc88x76', '13800000000', 'admin@example.com', 'ADMIN', 'ACTIVE', 0, 'NONE', now(), now())
on duplicate key update username = username;

insert into cars(brand, model, plate_no, price_per_day, deposit, status, is_special, seats, transmission, fuel_type, mileage, description, created_at, updated_at)
values
('丰田', '卡罗拉', '粤A12345', 200, 1000, 'AVAILABLE', 0, 5, 'AT', '汽油', 32000, '经济实用，适合通勤', now(), now()),
('特斯拉', 'Model 3', '粤B54321', 500, 2000, 'AVAILABLE', 1, 5, 'AT', '电动', 18000, '电动轿跑，舒适安静', now(), now()),
('本田', 'CR-V', '粤C67890', 350, 1500, 'AVAILABLE', 0, 5, 'AT', '汽油', 40000, '空间大，适合家庭出行', now(), now())
on duplicate key update plate_no = plate_no;
