CREATE TABLE IF NOT EXISTS `stock_in_orders` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `order_no`     VARCHAR(50)     NOT NULL,
  `type`         VARCHAR(30)     NOT NULL DEFAULT 'purchase',
  `warehouse_id` BIGINT UNSIGNED NOT NULL,
  `partner_id`   BIGINT UNSIGNED DEFAULT NULL,
  `operator_id`  BIGINT UNSIGNED NOT NULL,
  `reviewer_id`  BIGINT UNSIGNED DEFAULT NULL,
  `status`       ENUM('draft','pending','approved','rejected') NOT NULL DEFAULT 'draft',
  `order_date`   DATE            NOT NULL,
  `total_amount` DECIMAL(14,2)   NOT NULL DEFAULT 0,
  `reviewed_at`  DATETIME        DEFAULT NULL,
  `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`),
  UNIQUE KEY `uk_order_no` (`hotel_id`, `order_no`),
  KEY `idx_hotel_id` (`hotel_id`),
  KEY `idx_order_date` (`order_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `stock_in_items` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_id`     BIGINT UNSIGNED NOT NULL,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `product_id`   BIGINT UNSIGNED NOT NULL,
  `quantity`     DECIMAL(12,3)   NOT NULL,
  `unit_price`   DECIMAL(12,2)   NOT NULL DEFAULT 0,
  `total_price`  DECIMAL(14,2)   NOT NULL DEFAULT 0,
  `batch_no`     VARCHAR(50)     DEFAULT NULL,
  `expire_date`  DATE            DEFAULT NULL,
  `custom_fields` JSON           DEFAULT NULL,
  `remark`       VARCHAR(255)    DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `stock_out_orders` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `order_no`     VARCHAR(50)     NOT NULL,
  `type`         VARCHAR(30)     NOT NULL DEFAULT 'sale',
  `warehouse_id` BIGINT UNSIGNED NOT NULL,
  `partner_id`   BIGINT UNSIGNED DEFAULT NULL,
  `operator_id`  BIGINT UNSIGNED NOT NULL,
  `reviewer_id`  BIGINT UNSIGNED DEFAULT NULL,
  `status`       ENUM('draft','pending','approved','rejected') NOT NULL DEFAULT 'draft',
  `order_date`   DATE            NOT NULL,
  `total_amount` DECIMAL(14,2)   NOT NULL DEFAULT 0,
  `reviewed_at`  DATETIME        DEFAULT NULL,
  `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`),
  UNIQUE KEY `uk_order_no` (`hotel_id`, `order_no`),
  KEY `idx_hotel_id` (`hotel_id`),
  KEY `idx_order_date` (`order_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `stock_out_items` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_id`     BIGINT UNSIGNED NOT NULL,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `product_id`   BIGINT UNSIGNED NOT NULL,
  `quantity`     DECIMAL(12,3)   NOT NULL,
  `unit_price`   DECIMAL(12,2)   NOT NULL DEFAULT 0,
  `total_price`  DECIMAL(14,2)   NOT NULL DEFAULT 0,
  `custom_fields` JSON           DEFAULT NULL,
  `remark`       VARCHAR(255)    DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `inventory` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `warehouse_id` BIGINT UNSIGNED NOT NULL,
  `product_id`   BIGINT UNSIGNED NOT NULL,
  `quantity`     DECIMAL(12,3)   NOT NULL DEFAULT 0,
  `updated_at`   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hotel_wh_product` (`hotel_id`, `warehouse_id`, `product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `inventory_logs` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `warehouse_id` BIGINT UNSIGNED NOT NULL,
  `product_id`   BIGINT UNSIGNED NOT NULL,
  `direction`    ENUM('in','out') NOT NULL,
  `quantity`     DECIMAL(12,3)   NOT NULL,
  `qty_before`   DECIMAL(12,3)   NOT NULL,
  `qty_after`    DECIMAL(12,3)   NOT NULL,
  `ref_type`     VARCHAR(30)     NOT NULL,
  `ref_id`       BIGINT UNSIGNED NOT NULL,
  `operator_id`  BIGINT UNSIGNED DEFAULT NULL,
  `created_at`   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_hotel_product` (`hotel_id`, `product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `inventory_checks` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`     BIGINT UNSIGNED NOT NULL,
  `check_no`     VARCHAR(50)     NOT NULL,
  `warehouse_id` BIGINT UNSIGNED NOT NULL,
  `operator_id`  BIGINT UNSIGNED NOT NULL,
  `status`       ENUM('draft','completed') NOT NULL DEFAULT 'draft',
  `check_date`   DATE            NOT 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,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_check_no` (`hotel_id`, `check_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `inventory_check_items` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `check_id`    BIGINT UNSIGNED NOT NULL,
  `hotel_id`    BIGINT UNSIGNED NOT NULL,
  `product_id`  BIGINT UNSIGNED NOT NULL,
  `system_qty`  DECIMAL(12,3)  NOT NULL,
  `actual_qty`  DECIMAL(12,3)  NOT NULL,
  `diff_qty`    DECIMAL(12,3)  GENERATED ALWAYS AS (`actual_qty` - `system_qty`) STORED,
  `remark`      VARCHAR(255)   DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_check_id` (`check_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `finance_records` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`    BIGINT UNSIGNED NOT NULL,
  `partner_id`  BIGINT UNSIGNED DEFAULT NULL,
  `type`        ENUM('receivable','payable','receipt','payment') NOT NULL,
  `amount`      DECIMAL(14,2)   NOT NULL,
  `balance`     DECIMAL(14,2)   NOT NULL,
  `ref_type`    VARCHAR(30)     DEFAULT NULL,
  `ref_id`      BIGINT UNSIGNED DEFAULT NULL,
  `operator_id` BIGINT UNSIGNED NOT NULL,
  `record_date` DATE            NOT NULL,
  `custom_fields` JSON          DEFAULT NULL,
  `remark`      VARCHAR(500)    DEFAULT NULL,
  `created_at`  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_hotel_id` (`hotel_id`),
  KEY `idx_record_date` (`record_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `number_rules` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`    BIGINT UNSIGNED NOT NULL,
  `type`        VARCHAR(30)     NOT NULL,
  `prefix`      VARCHAR(20)     DEFAULT '',
  `current_no`  INT             NOT NULL DEFAULT 0,
  `date_format` VARCHAR(20)     DEFAULT 'YYYYMMDD',
  `updated_at`  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hotel_type` (`hotel_id`, `type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
