CREATE TABLE IF NOT EXISTS `product_categories` (
  `id`        BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`  BIGINT UNSIGNED NOT NULL,
  `name`      VARCHAR(100)    NOT NULL,
  `parent_id` BIGINT UNSIGNED DEFAULT NULL,
  `sort_order` INT            NOT NULL DEFAULT 0,
  `created_at` DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME       DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_hotel_id` (`hotel_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `products` (
  `id`             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`       BIGINT UNSIGNED NOT NULL,
  `code`           VARCHAR(50)     DEFAULT NULL,
  `barcode`        VARCHAR(100)    DEFAULT NULL,
  `name`           VARCHAR(200)    NOT NULL,
  `series`         VARCHAR(100)    DEFAULT NULL,
  `spec`           VARCHAR(100)    DEFAULT NULL,
  `unit`           VARCHAR(20)     DEFAULT NULL,
  `category_id`    BIGINT UNSIGNED DEFAULT NULL,
  `brand`          VARCHAR(100)    DEFAULT NULL,
  `purchase_price` DECIMAL(12,2)   DEFAULT NULL,
  `sale_price`     DECIMAL(12,2)   DEFAULT NULL,
  `min_stock`      INT             DEFAULT 0,
  `custom_fields`  JSON            DEFAULT NULL,
  `remark`         VARCHAR(500)    DEFAULT NULL,
  `created_at`     DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`     DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at`     DATETIME        DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_hotel_id` (`hotel_id`),
  FULLTEXT KEY `ft_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `warehouses` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `name`         VARCHAR(100)    NOT NULL,
  `location`     VARCHAR(200)    DEFAULT NULL,
  `is_default`   TINYINT(1)      NOT NULL DEFAULT 0,
  `custom_fields` JSON           DEFAULT NULL,
  `created_at`   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at`   DATETIME        DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_hotel_id` (`hotel_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `partners` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `code`         VARCHAR(50)     DEFAULT NULL,
  `name`         VARCHAR(200)    NOT NULL,
  `type`         SET('supplier','customer') NOT NULL DEFAULT 'supplier',
  `contact`      VARCHAR(50)     DEFAULT NULL,
  `phone`        VARCHAR(30)     DEFAULT NULL,
  `address`      VARCHAR(255)    DEFAULT NULL,
  `bank_account` VARCHAR(100)    DEFAULT NULL,
  `credit_limit` DECIMAL(12,2)   DEFAULT NULL,
  `balance`      DECIMAL(12,2)   NOT NULL DEFAULT 0,
  `custom_fields` JSON           DEFAULT NULL,
  `remark`       VARCHAR(500)    DEFAULT NULL,
  `created_at`   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at`   DATETIME        DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_hotel_id` (`hotel_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
