-- Run this in your phpMyAdmin or MySQL CLI to add the required API tables.
-- Only creates tables if they don't already exist.

-- Cart Items Table
CREATE TABLE IF NOT EXISTS `cart_items` (
  `id`         INT UNSIGNED    NOT NULL AUTO_INCREMENT,
  `user_id`    INT UNSIGNED    NOT NULL,
  `product_id` INT UNSIGNED    NOT NULL,
  `quantity`   INT UNSIGNED    NOT NULL DEFAULT 1,
  `created_at` TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_user_product` (`user_id`, `product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Orders Table
CREATE TABLE IF NOT EXISTS `orders` (
  `id`               INT UNSIGNED    NOT NULL AUTO_INCREMENT,
  `user_id`          INT UNSIGNED    NOT NULL,
  `total_amount`     DECIMAL(10,2)   NOT NULL DEFAULT '0.00',
  `shipping_address` JSON,
  `payment_method`   VARCHAR(50)     NOT NULL DEFAULT 'cod',
  `status`           ENUM('pending','processing','shipped','delivered','cancelled') NOT NULL DEFAULT 'pending',
  `payment_status`   ENUM('pending','paid','failed','refunded') NOT NULL DEFAULT 'pending',
  `created_at`       TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`       TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Order Items Table
CREATE TABLE IF NOT EXISTS `order_items` (
  `id`         INT UNSIGNED    NOT NULL AUTO_INCREMENT,
  `order_id`   INT UNSIGNED    NOT NULL,
  `product_id` INT UNSIGNED    NOT NULL,
  `quantity`   INT UNSIGNED    NOT NULL DEFAULT 1,
  `price`      DECIMAL(10,2)   NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Wishlist Table
CREATE TABLE IF NOT EXISTS `wishlist` (
  `id`         INT UNSIGNED    NOT NULL AUTO_INCREMENT,
  `user_id`    INT UNSIGNED    NOT NULL,
  `product_id` INT UNSIGNED    NOT NULL,
  `created_at` TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_user_product_wishlist` (`user_id`, `product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Addresses Table
CREATE TABLE IF NOT EXISTS `addresses` (
  `id`           INT UNSIGNED    NOT NULL AUTO_INCREMENT,
  `user_id`      INT UNSIGNED    NOT NULL,
  `address_type` ENUM('home', 'work', 'other') NOT NULL DEFAULT 'home',
  `full_name`    VARCHAR(255)    NOT NULL,
  `phone`        VARCHAR(20)     NOT NULL,
  `address`      TEXT            NOT NULL,
  `landmark`     VARCHAR(255),
  `city`         VARCHAR(100)    NOT NULL,
  `state`        VARCHAR(100)    NOT NULL,
  `pincode`      VARCHAR(10)     NOT NULL,
  `is_default`   BOOLEAN         NOT NULL DEFAULT FALSE,
  `created_at`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
