CREATE TABLE item_make_pizza (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    charge_type_flavor TINYINT DEFAULT 0 NOT NULL,
    active TINYINT(1) DEFAULT 1 NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE item_make_pizza_size (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    item_make_pizza_id BIGINT UNSIGNED NOT NULL,
    name VARCHAR(50) NULL,
    price DECIMAL(10,2) NULL,
    amount_slices TINYINT NULL,
    amount_flavors TINYINT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE item_make_pizza_flavor (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    item_make_pizza_id BIGINT UNSIGNED NOT NULL,
    name VARCHAR(50) NULL,
    description VARCHAR(200) NULL,
    price DECIMAL(10,2) NULL,
    img VARCHAR(100) NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

ALTER TABLE `plans` ADD `make_pizza` TINYINT NOT NULL DEFAULT '1' AFTER `loyalty`;

ALTER TABLE `loyalty_programs` ADD `top_awards` TINYINT NOT NULL DEFAULT '0' AFTER `cycle_points_months`;

CREATE TABLE loyalty_awards (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    program_id BIGINT UNSIGNED NOT NULL,
    placement TINYINT NOT NULL,
    type VARCHAR(50) NOT NULL,
    settings TEXT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

ALTER TABLE `coupon` CHANGE `category_id` `category_id` INT NOT NULL DEFAULT '-1', CHANGE `item_id` `item_id` INT NOT NULL DEFAULT '-1';
ALTER TABLE `coupon` CHANGE `min_value` `min_value` DECIMAL(10,2) NOT NULL DEFAULT '0', CHANGE `max_value` `max_value` DECIMAL(10,2) NOT NULL DEFAULT '0';
ALTER TABLE `coupon` CHANGE `reuse` `reuse` TINYINT NOT NULL DEFAULT '0', CHANGE `alert_client` `alert_client` TINYINT NOT NULL DEFAULT '0';

ALTER TABLE `users` ADD `photo` VARCHAR(50) NULL DEFAULT NULL AFTER `phone_number`;

ALTER TABLE `printer` ADD `name` VARCHAR(50) NULL DEFAULT NULL AFTER `spacing_type`, ADD `nickname` VARCHAR(50) NULL DEFAULT NULL AFTER `name`, ADD `default` TINYINT NOT NULL DEFAULT '0' AFTER `nickname`;
UPDATE `printer` SET `default` = 1;

ALTER TABLE `category` ADD `printers` TEXT NULL AFTER `hours`;

CREATE INDEX idx_printed ON orders(printed);
CREATE INDEX idx_company_id ON bill(company_id);

ALTER TABLE company ADD settings JSON NULL DEFAULT NULL AFTER security_confirm_phone_number;

CREATE TABLE mp_payments (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    data_id VARCHAR(40) NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE mp_payments ADD UNIQUE KEY unique_data_id (data_id);

CREATE TABLE addons_settings (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    slug VARCHAR(50) NOT NULL,
    settings TEXT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS `sessions` (
  `id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `ip_address` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `user_agent` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `last_activity` int NOT NULL,
  PRIMARY KEY (`id`),
  KEY `sessions_user_id_index` (`user_id`),
  KEY `sessions_last_activity_index` (`last_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE combo
ADD COLUMN IF NOT EXISTS img VARCHAR(255) NULL;
