<?php

namespace EasyRestaurantManager\Database\Migrations;

class Installer
{
    public function install()
    {
        global $wpdb;
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        $charset_collate = $wpdb->get_charset_collate();




        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_branches` (
            `id` INT AUTO_INCREMENT PRIMARY KEY,
            `name` VARCHAR(255) NOT NULL,
            `address` TEXT NOT NULL,
            `phone` VARCHAR(255) NOT NULL,
            `opening_hours` VARCHAR(255) NOT NULL,
            `status` VARCHAR(255) NOT NULL,
            `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_users` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            branch_id INT DEFAULT NULL,
            name VARCHAR(255) NOT NULL,
            email VARCHAR(255) UNIQUE NOT NULL,
            phone VARCHAR(20) NOT NULL,
            password VARCHAR(255) NOT NULL,
            role ENUM('customer', 'admin', 'manager') NOT NULL DEFAULT 'customer',
            loyalty_points INT DEFAULT 0,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_menu_categories` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            branch_id INT DEFAULT NULL,
            name VARCHAR(255) NOT NULL,
            image TEXT DEFAULT NULL,
            description TEXT,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_menu_items` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            branch_id INT DEFAULT NULL,
            category_id INT NOT NULL,
            post_id INT DEFAULT NULL,
            name VARCHAR(255) NOT NULL,
            description TEXT,
            base_price DECIMAL(10,2) NOT NULL,
            portion_sizes JSON DEFAULT NULL,
            addons JSON DEFAULT NULL,
            image VARCHAR(255) DEFAULT NULL,
            availability TEXT DEFAULT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_tables` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            branch_id INT DEFAULT NULL,
            table_number VARCHAR(20) UNIQUE NOT NULL,
            capacity INT NOT NULL,
            status ENUM('available', 'reserved', 'occupied') NOT NULL DEFAULT 'available',
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_orders` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            branch_id INT DEFAULT NULL,
            customer_id INT DEFAULT NULL,
            table_id INT DEFAULT NULL,
            customer_name VARCHAR(255) NOT NULL,
            phone VARCHAR(20) NOT NULL,
            address TEXT DEFAULT NULL,
            total_price DECIMAL(10,2) NOT NULL,
            discount_id INT DEFAULT NULL,
            tax_amount DECIMAL(10,2) DEFAULT 0.00,
            status ENUM('pending', 'completed', 'cancelled') NOT NULL DEFAULT 'pending',
            order_type ENUM('dine-in', 'takeaway', 'delivery') NOT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_order_items` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            order_id INT NOT NULL,
            menu_item_id INT NOT NULL,
            quantity INT NOT NULL,
            price DECIMAL(10,2) NOT NULL,
            selected_size VARCHAR(50),
            selected_addons JSON DEFAULT NULL,
            special_instructions TEXT DEFAULT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_table_reservations` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            branch_id INT DEFAULT NULL,
            customer_id INT DEFAULT NULL,
            customer_name VARCHAR(255) NOT NULL,
            phone VARCHAR(20) NOT NULL,
            email VARCHAR(255) DEFAULT NULL,
            table_id INT NOT NULL,
            reservation_date DATE NOT NULL,
            reservation_time TIME NOT NULL,
            num_guests INT NOT NULL,
            status ENUM('pending', 'confirmed', 'cancelled') NOT NULL DEFAULT 'pending',
            notes text  NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_transactions` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            order_id INT NOT NULL,
            payment_method ENUM('cash', 'card', 'online') NOT NULL,
            transaction_id VARCHAR(255) DEFAULT NULL,
            amount DECIMAL(10,2) NOT NULL,
            status ENUM('pending', 'successful', 'failed') NOT NULL DEFAULT 'pending',
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_coupons` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            name VARCHAR(50) NOT NULL,
            code VARCHAR(50) UNIQUE NOT NULL,
            discount_type ENUM('percentage', 'fixed') NOT NULL,
            discount_value DECIMAL(10,2) NOT NULL,
            added_date DATE NOT NULL,
            expiration_date DATE NOT NULL,
            status VARCHAR(50) NOT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}erm_reviews` (
            id INT PRIMARY KEY AUTO_INCREMENT,
            customer_id INT NOT NULL,
            menu_item_id INT NOT NULL,
            rating INT,
            review TEXT DEFAULT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";
        dbDelta($sql);

        //===========================
     
    }

    public function uninstall()
    {
        global $wpdb;

        $tables = [
            'erm_branches',
            'erm_users',
            'erm_menu_categories',
            'erm_menu_items',
            'erm_tables',
            'erm_orders',
            'erm_order_items',
            'erm_table_reservations',
            'erm_transactions',
            'erm_coupons',
            'erm_reviews',
            'erm_color_settings',
        ];

        foreach ($tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}{$table}");
        }
    }
}
