CREATE TABLE IF NOT EXISTS `mercyv_shops` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `shop_id` VARCHAR(50) NOT NULL UNIQUE,
    `owner_identifier` VARCHAR(60) DEFAULT NULL,
    `owner_name` VARCHAR(100) DEFAULT NULL,
    `shop_name` VARCHAR(100) NOT NULL,
    `vault_money` INT NOT NULL DEFAULT 0,
    `is_for_sale` TINYINT(1) NOT NULL DEFAULT 0,
    `sale_price` INT NOT NULL DEFAULT 0,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX `idx_owner` (`owner_identifier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `mercyv_shop_employees` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `shop_id` VARCHAR(50) NOT NULL,
    `employee_identifier` VARCHAR(60) NOT NULL,
    `employee_name` VARCHAR(100) NOT NULL,
    `added_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_shop` (`shop_id`),
    INDEX `idx_employee` (`employee_identifier`),
    UNIQUE KEY `uq_shop_employee` (`shop_id`, `employee_identifier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `mercyv_shop_shelves` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `shop_id` VARCHAR(50) NOT NULL,
    `item_name` VARCHAR(50) NOT NULL,
    `item_label` VARCHAR(100) NOT NULL,
    `price` INT NOT NULL,
    `quantity` INT NOT NULL DEFAULT 0,
    INDEX `idx_shop_shelves` (`shop_id`),
    UNIQUE KEY `uq_shop_item` (`shop_id`, `item_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `mercyv_shop_storage` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `shop_id` VARCHAR(50) NOT NULL,
    `item_name` VARCHAR(50) NOT NULL,
    `item_label` VARCHAR(100) NOT NULL,
    `quantity` INT NOT NULL DEFAULT 0,
    INDEX `idx_shop_storage` (`shop_id`),
    UNIQUE KEY `uq_shop_storage_item` (`shop_id`, `item_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `mercyv_shop_licenses` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `identifier` VARCHAR(60) NOT NULL,
    `license_name` VARCHAR(50) NOT NULL,
    `purchased_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uq_player_license` (`identifier`, `license_name`),
    INDEX `idx_identifier` (`identifier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `mercyv_shop_sales_history` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `shop_id` VARCHAR(50) NOT NULL,
    `buyer_identifier` VARCHAR(60) NOT NULL,
    `buyer_name` VARCHAR(100) NOT NULL,
    `item_name` VARCHAR(50) NOT NULL,
    `item_label` VARCHAR(100) NOT NULL,
    `price` INT NOT NULL,
    `quantity` INT NOT NULL,
    `total` INT NOT NULL,
    `sold_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_shop_sales` (`shop_id`),
    INDEX `idx_sold_at` (`sold_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
