CREATE TABLE IF NOT EXISTS schema_migrations (version VARCHAR(100) PRIMARY KEY, applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS users (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL,
 email VARCHAR(191) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL,
 role ENUM('customer','admin','order_taker') NOT NULL DEFAULT 'customer', phone VARCHAR(30) DEFAULT '',
 email_verified BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS sessions (
 token_hash CHAR(64) PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, expires_at DATETIME NOT NULL,
 FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, INDEX (expires_at)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS account_tokens (
 token_hash CHAR(64) PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL,
 purpose ENUM('verify','reset') NOT NULL, expires_at DATETIME NOT NULL,
 FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS categories (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL,
 slug VARCHAR(150) NOT NULL UNIQUE, parent_id BIGINT UNSIGNED NULL,
 description VARCHAR(255) DEFAULT '', icon VARCHAR(30) DEFAULT 'package', sort_order INT DEFAULT 0,
 FOREIGN KEY (parent_id) REFERENCES categories(id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS products (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, category_id BIGINT UNSIGNED NOT NULL,
 name VARCHAR(191) NOT NULL, slug VARCHAR(191) NOT NULL UNIQUE, description TEXT NOT NULL,
 brand VARCHAR(100) DEFAULT '', image_url VARCHAR(1000) DEFAULT '', images JSON,
 specifications JSON, storage_type ENUM('ambient','chilled','frozen') DEFAULT 'ambient',
 storage_instructions TEXT, featured BOOLEAN DEFAULT FALSE, is_active BOOLEAN DEFAULT TRUE,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 FOREIGN KEY (category_id) REFERENCES categories(id), INDEX (is_active, category_id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product_variants (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_id BIGINT UNSIGNED NOT NULL,
 name VARCHAR(150) NOT NULL, sku VARCHAR(100) NOT NULL UNIQUE, price DECIMAL(12,2) NOT NULL,
 stock_quantity INT UNSIGNED NOT NULL DEFAULT 0, min_quantity INT UNSIGNED DEFAULT 1,
 weight_grams INT UNSIGNED DEFAULT 0, is_active BOOLEAN DEFAULT TRUE,
 FOREIGN KEY (product_id) REFERENCES products(id), CHECK (price > 0), CHECK (min_quantity > 0)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS store_settings (
 id INT PRIMARY KEY, accepting_orders BOOLEAN DEFAULT FALSE, minimum_order DECIMAL(12,2) DEFAULT 0,
 delivery_fee DECIMAL(12,2) DEFAULT 0, free_delivery_above DECIMAL(12,2) DEFAULT 0,
 delivery_cities JSON NOT NULL, frozen_cities JSON NOT NULL, pickup_enabled BOOLEAN DEFAULT FALSE,
 pickup_address VARCHAR(500) DEFAULT '', dispatch_note VARCHAR(500) DEFAULT ''
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS orders (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL,
 request_key VARCHAR(100) NOT NULL, name VARCHAR(100) NOT NULL, phone VARCHAR(30) NOT NULL,
 city VARCHAR(100) NOT NULL DEFAULT '', address VARCHAR(1000) NOT NULL DEFAULT '', note VARCHAR(1000) DEFAULT '',
 order_type ENUM('delivery','pickup') NOT NULL, payment_method VARCHAR(30) NOT NULL DEFAULT 'cash',
 payment_status ENUM('unpaid','paid') DEFAULT 'unpaid',
 status ENUM('pending','confirmed','packing','dispatched','ready_for_pickup','delivered','cancelled') DEFAULT 'pending',
 items_total DECIMAL(12,2) NOT NULL, delivery_charges DECIMAL(12,2) NOT NULL, total_price DECIMAL(12,2) NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 FOREIGN KEY (user_id) REFERENCES users(id), UNIQUE (user_id, request_key), INDEX (status, created_at)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS order_items (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, order_id BIGINT UNSIGNED NOT NULL,
 variant_id BIGINT UNSIGNED NOT NULL, product_name VARCHAR(191) NOT NULL, variant_name VARCHAR(150) NOT NULL,
 sku VARCHAR(100) NOT NULL, image_url VARCHAR(1000) DEFAULT '', quantity INT UNSIGNED NOT NULL,
 unit_price DECIMAL(12,2) NOT NULL,
 FOREIGN KEY (order_id) REFERENCES orders(id), FOREIGN KEY (variant_id) REFERENCES product_variants(id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS stock_movements (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, variant_id BIGINT UNSIGNED NOT NULL,
 quantity_change INT NOT NULL, reason VARCHAR(255) NOT NULL, order_id BIGINT UNSIGNED NULL,
 user_id BIGINT UNSIGNED NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (variant_id) REFERENCES product_variants(id), FOREIGN KEY (order_id) REFERENCES orders(id),
 FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS order_events (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, order_id BIGINT UNSIGNED NOT NULL,
 status VARCHAR(40) NOT NULL, user_id BIGINT UNSIGNED NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (order_id) REFERENCES orders(id), FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB;
INSERT IGNORE INTO store_settings (id, delivery_cities, frozen_cities, dispatch_note) VALUES (1, JSON_ARRAY(), JSON_ARRAY(), 'Delivery availability and charges are shown at checkout.');
