-- AFG CARS - Premium Car Marketplace Afghanistan (2026 Edition)
-- Database Schema for XAMPP (Local Edition)

CREATE DATABASE IF NOT EXISTS afg_cars;
USE afg_cars;

-- Users Table
CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin', 'user') DEFAULT 'user',
    status ENUM('active', 'inactive') DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- Cars Table
CREATE TABLE IF NOT EXISTS cars (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NULL,
    title VARCHAR(255) NOT NULL,
    brand VARCHAR(100) NOT NULL,
    model VARCHAR(100) NOT NULL,
    year INT NOT NULL,
    price DECIMAL(12, 2) NOT NULL,
    location VARCHAR(100) NOT NULL,
    fuel_type VARCHAR(50),
    transmission VARCHAR(50),
    mileage INT,
    description TEXT,
    image VARCHAR(255),
    status ENUM('pending', 'approved', 'rejected', 'sold') DEFAULT 'pending',
    badge VARCHAR(50) NULL,
    view_count INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB;

-- Favorites Table
CREATE TABLE IF NOT EXISTS favorites (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    car_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY (user_id, car_id),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Purchases Table
CREATE TABLE IF NOT EXISTS purchases (
    id INT AUTO_INCREMENT PRIMARY KEY,
    car_id INT NOT NULL,
    buyer_id INT NOT NULL,
    payment_method VARCHAR(50) NOT NULL,
    payment_status ENUM('pending', 'completed', 'failed') DEFAULT 'pending',
    total_amount DECIMAL(12, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE,
    FOREIGN KEY (buyer_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Reviews Table
CREATE TABLE IF NOT EXISTS reviews (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    car_id INT NOT NULL,
    rating INT NOT NULL CHECK (rating >= 1 AND rating <= 5),
    comment TEXT,
    status ENUM('active', 'inactive') DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Notifications Table
CREATE TABLE IF NOT EXISTS notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    type VARCHAR(50) DEFAULT 'info',
    message TEXT NOT NULL,
    status ENUM('unread', 'read') DEFAULT 'unread',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- Branches Table
CREATE TABLE IF NOT EXISTS branches (
    id INT AUTO_INCREMENT PRIMARY KEY,
    city VARCHAR(100) NOT NULL,
    address VARCHAR(255) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    working_hours VARCHAR(100) NOT NULL
) ENGINE=InnoDB;

-- Insert Default Admin (Password: 12345678)
INSERT INTO users (name, email, password, role, status) 
VALUES ('System Admin', 'admin@gmail.com', '$2y$10$L19wS0h4W7mP2e3R4t5Y6u7I8o9P0a1S2d3F4g5H6j7K8l9Z0x1C2', 'admin', 'active')
ON DUPLICATE KEY UPDATE role='admin';

-- Insert Sample Branches
INSERT INTO branches (city, address, phone, working_hours) VALUES
('Kabul', 'Main Square, District 4, Kabul', '+93 700 123 456', 'Sat-Thu: 8:00 AM - 5:00 PM'),
('Herat', 'Silk Road St, Herat City', '+93 700 654 321', 'Sat-Thu: 8:00 AM - 4:00 PM'),
('Mazar-i-Sharif', 'Balkh Gate Ave, Mazar', '+93 700 111 222', 'Sat-Thu: 8:00 AM - 5:00 PM'),
('Kandahar', 'Ahmad Shah Baba Blvd, Kandahar', '+93 700 333 444', 'Sat-Thu: 8:00 AM - 4:00 PM');