CREATE TABLE IF NOT EXISTS wh_stocktake (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  stocktake_no VARCHAR(50) NOT NULL UNIQUE,
  status VARCHAR(32) NOT NULL,
  started_by_user_id BIGINT NULL,
  imported_by_user_id BIGINT NULL,
  confirmed_by_user_id BIGINT NULL,
  canceled_by_user_id BIGINT NULL,
  started_at DATETIME NOT NULL,
  exported_at DATETIME NULL,
  imported_at DATETIME NULL,
  confirmed_at DATETIME NULL,
  canceled_at DATETIME NULL,
  remark VARCHAR(255) NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_wh_stocktake_status (status),
  INDEX idx_wh_stocktake_started_at (started_at)
);

CREATE TABLE IF NOT EXISTS wh_stocktake_warehouse (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  stocktake_id BIGINT NOT NULL,
  warehouse_id BIGINT NOT NULL,
  warehouse_type VARCHAR(32) NOT NULL,
  warehouse_name VARCHAR(100) NOT NULL,
  status VARCHAR(32) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_wh_stocktake_warehouse_stocktake (stocktake_id),
  INDEX idx_wh_stocktake_warehouse_lock (warehouse_id, status),
  CONSTRAINT fk_stocktake_warehouse_stocktake FOREIGN KEY (stocktake_id) REFERENCES wh_stocktake(id),
  CONSTRAINT fk_stocktake_warehouse_warehouse FOREIGN KEY (warehouse_id) REFERENCES wh_warehouse(id)
);

CREATE TABLE IF NOT EXISTS wh_stocktake_line (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  stocktake_id BIGINT NOT NULL,
  stocktake_warehouse_id BIGINT NULL,
  line_no INT NOT NULL,
  warehouse_id BIGINT NOT NULL,
  warehouse_name VARCHAR(100) NOT NULL,
  warehouse_type VARCHAR(32) NOT NULL,
  location_id BIGINT NULL,
  location_name VARCHAR(100) NULL,
  item_id BIGINT NOT NULL,
  item_code VARCHAR(50) NOT NULL,
  item_name VARCHAR(200) NOT NULL,
  item_type VARCHAR(32) NOT NULL,
  lot_id BIGINT NULL,
  lot_no VARCHAR(80) NULL,
  source_material_sub_batch_no VARCHAR(80) NULL,
  snapshot_qty DECIMAL(18,6) NOT NULL DEFAULT 0,
  snapshot_weight_kg DECIMAL(18,6) NOT NULL DEFAULT 0,
  snapshot_unit_cost DECIMAL(18,4) NOT NULL DEFAULT 0,
  counted_qty DECIMAL(18,6) NULL,
  counted_weight_kg DECIMAL(18,6) NULL,
  diff_qty DECIMAL(18,6) NOT NULL DEFAULT 0,
  diff_weight_kg DECIMAL(18,6) NOT NULL DEFAULT 0,
  diff_amount DECIMAL(18,2) NOT NULL DEFAULT 0,
  diff_type VARCHAR(32) NOT NULL,
  row_status VARCHAR(32) NOT NULL,
  error_message VARCHAR(500) NULL,
  remark VARCHAR(255) NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_wh_stocktake_line_stocktake (stocktake_id),
  INDEX idx_wh_stocktake_line_lot (lot_id),
  INDEX idx_wh_stocktake_line_diff (diff_type, row_status),
  CONSTRAINT fk_stocktake_line_stocktake FOREIGN KEY (stocktake_id) REFERENCES wh_stocktake(id),
  CONSTRAINT fk_stocktake_line_warehouse FOREIGN KEY (warehouse_id) REFERENCES wh_warehouse(id),
  CONSTRAINT fk_stocktake_line_item FOREIGN KEY (item_id) REFERENCES md_item(id)
);

CREATE TABLE IF NOT EXISTS wh_stocktake_adjustment (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  stocktake_id BIGINT NOT NULL,
  stocktake_line_id BIGINT NOT NULL,
  warehouse_id BIGINT NOT NULL,
  item_id BIGINT NOT NULL,
  lot_id BIGINT NULL,
  adjustment_lot_id BIGINT NULL,
  inventory_txn_id BIGINT NULL,
  adjustment_type VARCHAR(32) NOT NULL,
  before_qty DECIMAL(18,6) NOT NULL DEFAULT 0,
  after_qty DECIMAL(18,6) NOT NULL DEFAULT 0,
  before_weight_kg DECIMAL(18,6) NOT NULL DEFAULT 0,
  after_weight_kg DECIMAL(18,6) NOT NULL DEFAULT 0,
  qty_change DECIMAL(18,6) NOT NULL DEFAULT 0,
  weight_change_kg DECIMAL(18,6) NOT NULL DEFAULT 0,
  unit_cost DECIMAL(18,4) NOT NULL DEFAULT 0,
  amount DECIMAL(18,2) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_wh_stocktake_adjustment_stocktake (stocktake_id),
  CONSTRAINT fk_stocktake_adjustment_stocktake FOREIGN KEY (stocktake_id) REFERENCES wh_stocktake(id),
  CONSTRAINT fk_stocktake_adjustment_line FOREIGN KEY (stocktake_line_id) REFERENCES wh_stocktake_line(id)
);
