ALTER TABLE `company` ADD `switch_print_separate_numbering` TINYINT NOT NULL DEFAULT '0' AFTER `switch_print_group_items`;

CREATE TABLE `password_reset_tokens` (
  `email` varchar(255) NOT NULL,
  `token` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Programas de fidelidade (um por loja)
CREATE TABLE loyalty_programs (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    name VARCHAR(150) NOT NULL,
    description TEXT NULL,
    cycle_points_type TINYINT(1) DEFAULT 1,
    cycle_points_months SMALLINT DEFAULT 4,
    active TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Níveis do programa (Bronze, Prata, Ouro…)
CREATE TABLE loyalty_levels (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    program_id BIGINT UNSIGNED NOT NULL,
    name VARCHAR(100) NOT NULL,
    min_points INT UNSIGNED NOT NULL,
    benefits TEXT NULL, -- Ex: {"frete_gratis":true,"cashback":10}
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Missões/Desafios (ex: peça 5 vezes, peça sobremesa, etc.)
CREATE TABLE loyalty_missions (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    program_id BIGINT UNSIGNED NOT NULL,
    title VARCHAR(150) NOT NULL,
    description VARCHAR(255) NULL,
    levels TEXT NOT NULL,
    type VARCHAR(100) NOT NULL,
    conditions TEXT NOT NULL, -- Ex: {"tipo":"produto","produto_id":12,"quantidade":3,"prazo":"2025-12-31"}
    frequency ENUM('daily','weekly','monthly','once') DEFAULT 'once' NOT NULL,
    reward_points INT UNSIGNED NOT NULL,
    active TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Pontos do cliente (histórico)
CREATE TABLE loyalty_points (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    program_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    points INT NOT NULL,
    source VARCHAR(50) NOT NULL, -- Ex: 'pedido', 'missao', 'bonus_manual'
    expires_at TIMESTAMP NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Recompensas/Prêmios
CREATE TABLE loyalty_rewards (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    program_id BIGINT UNSIGNED NOT NULL,
    level_id BIGINT UNSIGNED NULL,
    reward_type VARCHAR(50) NOT NULL, -- Ex: 'cupom','frete_gratis','cashback','brinde'
    value VARCHAR(255) NULL, -- Pode ser % desconto, código de cupom, descrição do brinde
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE loyalty_mission_progress (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    mission_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    progress DECIMAL(12,2) DEFAULT 0, -- Pode armazenar quantidade ou valor monetário
    completed TINYINT(1) DEFAULT 0,  -- Missão concluída?
    last_completed_at TIMESTAMP NULL, -- Última vez que o progresso foi registrado
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

ALTER TABLE `coupon_used` ADD `order_id` INT NULL AFTER `user_client_id`, ADD `loyalty_program_id` INT NULL AFTER `order_id`;

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

ALTER TABLE `company` ADD `switch_qrcode_only_consult` TINYINT(1) NOT NULL DEFAULT '0' AFTER `seo_settings`;

CREATE TABLE blocked_customers (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    phone_number VARCHAR(25) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

ALTER TABLE `company` ADD `security_confirm_phone_number` TINYINT(1) NOT NULL DEFAULT '0' AFTER `switch_qrcode_only_consult`;
