-- Trimsy — estrutura das tabelas
-- Banco: procloud_trimsy
-- So cria tabelas. Nao copia usuarios, empresas nem agendamentos.
-- No phpMyAdmin, selecione procloud_trimsy e importe este arquivo.

CREATE DATABASE IF NOT EXISTS `procloud_trimsy`
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_unicode_ci;

USE `procloud_trimsy`;

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO';
CREATE TABLE IF NOT EXISTS `business_addresses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `title` varchar(200) NOT NULL,
  `subtitle` varchar(255) DEFAULT NULL,
  `number` varchar(20) NOT NULL,
  `complement` varchar(60) DEFAULT NULL,
  `lat` decimal(10,7) DEFAULT NULL,
  `lon` decimal(10,7) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_address_business` (`business_id`),
  CONSTRAINT `fk_address_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_agenda_blocks` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `reason` varchar(160) NOT NULL DEFAULT '',
  `start_ms` bigint(20) NOT NULL,
  `end_ms` bigint(20) NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `staff_id` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_blk` (`business_id`,`client_temp_id`),
  KEY `idx_ag_blk_biz` (`business_id`,`deleted_at`,`start_ms`),
  KEY `idx_blk_overlap` (`business_id`,`start_ms`,`end_ms`),
  CONSTRAINT `fk_blk_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_agenda_time_off` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `staff_id` varchar(64) DEFAULT NULL,
  `reason` varchar(80) NOT NULL DEFAULT 'dayoff',
  `all_day` tinyint(1) NOT NULL DEFAULT 1,
  `start_ms` bigint(20) NOT NULL,
  `end_ms` bigint(20) NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_off` (`business_id`,`client_temp_id`),
  KEY `idx_ag_off_biz` (`business_id`,`deleted_at`,`start_ms`),
  KEY `idx_off_overlap` (`business_id`,`staff_id`,`start_ms`,`end_ms`),
  CONSTRAINT `fk_off_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_appointments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `client_local_id` varchar(64) DEFAULT NULL,
  `client_name` varchar(160) NOT NULL DEFAULT '',
  `service_id` varchar(64) DEFAULT NULL,
  `service_name` varchar(160) NOT NULL DEFAULT '',
  `start_ms` bigint(20) NOT NULL,
  `end_ms` bigint(20) NOT NULL,
  `status` varchar(24) NOT NULL DEFAULT 'confirmed',
  `created_by` varchar(24) NOT NULL DEFAULT 'staff',
  `short_id` varchar(16) DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `staff_id` varchar(64) NOT NULL DEFAULT 'owner',
  `source` varchar(24) NOT NULL DEFAULT 'staff',
  `cancelled_at` timestamp NULL DEFAULT NULL,
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  `trimsy_client_id` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_apt` (`business_id`,`client_temp_id`),
  KEY `idx_ag_apt_biz` (`business_id`,`deleted_at`,`start_ms`),
  KEY `idx_apt_overlap` (`business_id`,`staff_id`,`start_ms`,`end_ms`),
  CONSTRAINT `fk_apt_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_audit_log` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL,
  `entity` varchar(40) NOT NULL,
  `entity_id` varchar(64) NOT NULL DEFAULT '',
  `action` varchar(24) NOT NULL,
  `payload_json` longtext DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `before_json` longtext DEFAULT NULL,
  `source` varchar(40) DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  `user_agent` varchar(180) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_ag_aud_biz` (`business_id`,`created_at`),
  KEY `idx_ag_aud_ent` (`entity`,`entity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_clients` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `first_name` varchar(80) NOT NULL DEFAULT '',
  `last_name` varchar(80) NOT NULL DEFAULT '',
  `phone` varchar(32) DEFAULT NULL,
  `email` varchar(120) DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_client` (`business_id`,`client_temp_id`),
  KEY `idx_ag_cli_biz` (`business_id`,`deleted_at`),
  CONSTRAINT `fk_cli_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_day_exceptions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `day_ymd` date NOT NULL,
  `is_open` tinyint(1) NOT NULL DEFAULT 0,
  `open_time` time DEFAULT NULL,
  `close_time` time DEFAULT NULL,
  `has_pause` tinyint(1) NOT NULL DEFAULT 0,
  `pause_from` time DEFAULT NULL,
  `pause_to` time DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_day_exc` (`business_id`,`day_ymd`),
  KEY `idx_day_exc_biz` (`business_id`,`day_ymd`),
  CONSTRAINT `fk_day_exc_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_expenses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `category` varchar(32) NOT NULL DEFAULT 'other',
  `note` varchar(200) NOT NULL DEFAULT '',
  `amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `spent_ms` bigint(20) NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_exp` (`business_id`,`client_temp_id`),
  KEY `idx_exp_biz` (`business_id`,`deleted_at`,`spent_ms`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_highlight_likes` (
  `highlight_id` bigint(20) unsigned NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`highlight_id`,`user_id`),
  KEY `idx_hll_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_highlight_messages` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `highlight_id` bigint(20) unsigned NOT NULL,
  `from_user_id` bigint(20) unsigned NOT NULL,
  `to_user_id` bigint(20) unsigned NOT NULL,
  `body` varchar(280) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_hlm_hl` (`highlight_id`,`created_at`),
  KEY `idx_hlm_to` (`to_user_id`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_highlight_views` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `highlight_id` bigint(20) unsigned NOT NULL,
  `business_id` bigint(20) unsigned NOT NULL,
  `viewer_key` varchar(80) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_hlv_highlight` (`highlight_id`),
  KEY `idx_hlv_biz` (`business_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_highlights` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `professional_id` bigint(20) unsigned DEFAULT NULL,
  `title` varchar(80) NOT NULL,
  `description` varchar(280) DEFAULT NULL,
  `video_path` varchar(255) NOT NULL,
  `thumbnail_path` varchar(255) DEFAULT NULL,
  `duration_sec` int(10) unsigned DEFAULT NULL,
  `status` enum('published','draft','archived') NOT NULL DEFAULT 'published',
  `views_count` int(10) unsigned NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_hl_business_status` (`business_id`,`status`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_hours` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `day_id` enum('seg','ter','qua','qui','sex','sab','dom') NOT NULL,
  `day_label` varchar(40) NOT NULL,
  `is_open` tinyint(1) NOT NULL DEFAULT 0,
  `open_time` time DEFAULT NULL,
  `close_time` time DEFAULT NULL,
  `has_pause` tinyint(1) NOT NULL DEFAULT 0,
  `pause_from` time DEFAULT NULL,
  `pause_to` time DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hours_day` (`business_id`,`day_id`),
  CONSTRAINT `fk_hours_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_inbox` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `item_key` varchar(80) NOT NULL,
  `type` varchar(24) NOT NULL DEFAULT 'book',
  `title` varchar(160) NOT NULL,
  `body` varchar(280) NOT NULL DEFAULT '',
  `apt_id` varchar(64) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_inbox_item` (`business_id`,`item_key`),
  KEY `idx_biz_inbox_created` (`business_id`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_portfolio_likes` (
  `business_id` bigint(20) unsigned NOT NULL,
  `photo_key` varchar(191) NOT NULL,
  `client_id` bigint(20) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`business_id`,`photo_key`,`client_id`),
  KEY `idx_bpl_photo` (`business_id`,`photo_key`),
  KEY `idx_bpl_client` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_reviews` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_id` bigint(20) unsigned DEFAULT NULL,
  `rating` tinyint(3) unsigned NOT NULL,
  `comment` text DEFAULT NULL,
  `author_name` varchar(160) NOT NULL DEFAULT '',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `service_name` varchar(160) DEFAULT NULL,
  `staff_name` varchar(160) DEFAULT NULL,
  `photo_path` varchar(255) DEFAULT NULL,
  `photos_json` text DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_rev_biz` (`business_id`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_sales` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `created_by_user_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `client_local_id` varchar(64) DEFAULT NULL,
  `trimsy_client_id` bigint(20) unsigned DEFAULT NULL,
  `client_name` varchar(160) NOT NULL DEFAULT '',
  `items_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`items_json`)),
  `subtotal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `discount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total` decimal(12,2) NOT NULL DEFAULT 0.00,
  `method` varchar(32) NOT NULL DEFAULT 'cash',
  `apt_id` varchar(64) DEFAULT NULL,
  `created_ms` bigint(20) NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_sale` (`business_id`,`client_temp_id`),
  KEY `idx_sale_biz` (`business_id`,`deleted_at`,`created_ms`),
  KEY `idx_sale_trim_client` (`trimsy_client_id`,`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `business_settings` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `setting_key` varchar(40) NOT NULL,
  `setting_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`setting_json`)),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_setting` (`business_id`,`setting_key`),
  CONSTRAINT `fk_settings_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `businesses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `owner_user_id` bigint(20) unsigned NOT NULL,
  `name` varchar(150) NOT NULL,
  `category` enum('barbeiros','manicure','cabeleireiro','sobrancelhas','pele','bemestar','biospa') NOT NULL,
  `work_location` enum('estabelecimento','cliente') NOT NULL DEFAULT 'estabelecimento',
  `sells_products` tinyint(1) NOT NULL DEFAULT 0,
  `onboarding_completed_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `country_code` char(2) NOT NULL DEFAULT 'BR',
  `currency_code` char(3) NOT NULL DEFAULT 'BRL',
  `locale` varchar(16) NOT NULL DEFAULT 'pt-BR',
  `subscription_status` varchar(32) NOT NULL DEFAULT 'none',
  `current_subscription_id` bigint(20) unsigned DEFAULT NULL,
  `staff_limit` smallint(5) unsigned NOT NULL DEFAULT 0,
  `trial_started_at` datetime DEFAULT NULL,
  `trial_ends_at` datetime DEFAULT NULL,
  `referred_by_business_id` bigint(20) unsigned DEFAULT NULL,
  `invite_code` varchar(16) DEFAULT NULL,
  `first_invoice_promo` tinyint(1) NOT NULL DEFAULT 0,
  `first_invoice_used` tinyint(1) NOT NULL DEFAULT 0,
  `gateway_code` varchar(40) DEFAULT NULL,
  `comp_forever` tinyint(1) NOT NULL DEFAULT 0,
  `comp_code` varchar(40) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_businesses_owner` (`owner_user_id`),
  UNIQUE KEY `uk_businesses_invite` (`invite_code`),
  CONSTRAINT `fk_businesses_owner` FOREIGN KEY (`owner_user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `checkout_sessions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `token` char(64) NOT NULL,
  `business_id` bigint(20) unsigned NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL,
  `plan_id` bigint(20) unsigned NOT NULL,
  `gateway_code` varchar(40) NOT NULL,
  `currency_code` char(3) NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `interval_unit` enum('month','year') NOT NULL DEFAULT 'month',
  `purpose` enum('subscribe','manage','change_plan') NOT NULL DEFAULT 'subscribe',
  `status` enum('pending','opened','completed','expired','cancelled') NOT NULL DEFAULT 'pending',
  `expires_at` datetime NOT NULL,
  `consumed_at` datetime DEFAULT NULL,
  `gateway_payload` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`gateway_payload`)),
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `list_amount` decimal(12,2) DEFAULT NULL,
  `discount_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `promo_code` varchar(40) DEFAULT NULL,
  `applied_credit_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`applied_credit_ids`)),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_checkout_token` (`token`),
  KEY `idx_checkout_biz` (`business_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_accounts` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(190) NOT NULL,
  `google_id` varchar(64) DEFAULT NULL,
  `facebook_id` varchar(64) DEFAULT NULL,
  `password_hash` varchar(255) NOT NULL DEFAULT '',
  `full_name` varchar(150) NOT NULL DEFAULT '',
  `phone` varchar(32) DEFAULT NULL,
  `birth_date` date DEFAULT NULL,
  `audience` varchar(16) NOT NULL DEFAULT 'all',
  `styles` varchar(255) NOT NULL DEFAULT '',
  `photo_path` varchar(255) DEFAULT NULL,
  `invite_code` varchar(16) DEFAULT NULL,
  `invited_by` bigint(20) unsigned DEFAULT NULL,
  `address_title` varchar(200) NOT NULL DEFAULT '',
  `address_subtitle` varchar(255) NOT NULL DEFAULT '',
  `address_lat` decimal(10,7) DEFAULT NULL,
  `address_lon` decimal(10,7) DEFAULT NULL,
  `notify_show` tinyint(1) NOT NULL DEFAULT 1,
  `notify_email` tinyint(1) NOT NULL DEFAULT 1,
  `notify_push` tinyint(1) NOT NULL DEFAULT 1,
  `country` varchar(8) NOT NULL DEFAULT 'BR',
  `status` enum('pending','active','inactive') NOT NULL DEFAULT 'pending',
  `lang` varchar(8) NOT NULL DEFAULT 'pt',
  `last_login_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_client_accounts_email` (`email`),
  UNIQUE KEY `uk_client_accounts_google` (`google_id`),
  UNIQUE KEY `uk_client_accounts_facebook` (`facebook_id`),
  UNIQUE KEY `uk_client_invite_code` (`invite_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_gift_orders` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_id` bigint(20) unsigned NOT NULL,
  `card_id` varchar(80) NOT NULL,
  `card_name` varchar(160) NOT NULL DEFAULT '',
  `card_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`card_json`)),
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  `credit_value` decimal(10,2) NOT NULL DEFAULT 0.00,
  `expire_days` int(11) NOT NULL DEFAULT 365,
  `recipient_name` varchar(160) NOT NULL DEFAULT '',
  `recipient_phone` varchar(32) NOT NULL DEFAULT '',
  `note` varchar(240) NOT NULL DEFAULT '',
  `token` char(64) NOT NULL,
  `status` enum('pending','approved','denied','cancelled') NOT NULL DEFAULT 'pending',
  `sale_id` varchar(80) DEFAULT NULL,
  `decided_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_gift_token` (`token`),
  KEY `idx_gift_biz` (`business_id`,`status`),
  KEY `idx_gift_client` (`client_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_inbox` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `client_id` bigint(20) unsigned NOT NULL,
  `business_id` bigint(20) unsigned DEFAULT NULL,
  `item_key` varchar(80) NOT NULL,
  `type` varchar(24) NOT NULL DEFAULT 'book',
  `title` varchar(160) NOT NULL,
  `body` varchar(280) NOT NULL DEFAULT '',
  `apt_id` varchar(64) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_cli_inbox_item` (`client_id`,`item_key`),
  KEY `idx_cli_inbox_created` (`client_id`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_invites` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `referrer_id` bigint(20) unsigned NOT NULL,
  `referred_id` bigint(20) unsigned NOT NULL,
  `invite_code` varchar(16) NOT NULL DEFAULT '',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_client_invite_referred` (`referred_id`),
  KEY `idx_client_invite_ref` (`referrer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_loyalty_cards` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_id` bigint(20) unsigned NOT NULL,
  `program_id` varchar(64) NOT NULL DEFAULT '',
  `stamps_have` int(11) NOT NULL DEFAULT 0,
  `stamps_need` int(11) NOT NULL DEFAULT 10,
  `reward` varchar(160) NOT NULL DEFAULT '',
  `design` varchar(24) NOT NULL DEFAULT 'classic',
  `min_value` decimal(10,2) NOT NULL DEFAULT 0.00,
  `status` enum('active','ready','redeemed') NOT NULL DEFAULT 'active',
  `last_sale_id` varchar(80) DEFAULT NULL,
  `last_stamp_at` timestamp NULL DEFAULT NULL,
  `ready_at` timestamp NULL DEFAULT NULL,
  `redeemed_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_loyal_client` (`client_id`,`status`),
  KEY `idx_loyal_biz` (`business_id`,`client_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_package_hires` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_id` bigint(20) unsigned NOT NULL,
  `package_id` varchar(80) NOT NULL,
  `package_name` varchar(160) NOT NULL DEFAULT '',
  `package_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`package_json`)),
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  `expire_days` int(11) NOT NULL DEFAULT 30,
  `token` char(64) NOT NULL,
  `status` enum('pending','approved','denied','cancelled') NOT NULL DEFAULT 'pending',
  `sale_id` varchar(80) DEFAULT NULL,
  `decided_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hire_token` (`token`),
  KEY `idx_hire_biz` (`business_id`,`status`),
  KEY `idx_hire_client` (`client_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_review_invites` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `apt_temp_id` varchar(64) NOT NULL,
  `client_account_id` bigint(20) unsigned DEFAULT NULL,
  `shop_name` varchar(160) NOT NULL DEFAULT '',
  `shop_logo` varchar(255) NOT NULL DEFAULT '',
  `service_name` varchar(160) NOT NULL DEFAULT '',
  `staff_name` varchar(160) NOT NULL DEFAULT '',
  `client_name` varchar(160) NOT NULL DEFAULT '',
  `status` varchar(16) NOT NULL DEFAULT 'pending',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_rev_invite_apt` (`business_id`,`apt_temp_id`),
  KEY `idx_rev_invite_client` (`client_account_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `client_support_messages` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `client_id` bigint(20) unsigned DEFAULT NULL,
  `topic` varchar(32) NOT NULL DEFAULT 'other',
  `message` text NOT NULL,
  `client_name` varchar(160) NOT NULL DEFAULT '',
  `client_email` varchar(190) NOT NULL DEFAULT '',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_help_client` (`client_id`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `countries` (
  `code` char(2) NOT NULL,
  `name` varchar(80) NOT NULL,
  `default_currency` char(3) NOT NULL,
  `locale` varchar(16) NOT NULL,
  `ddi` varchar(8) DEFAULT NULL,
  PRIMARY KEY (`code`),
  KEY `idx_countries_ddi` (`ddi`),
  KEY `fk_countries_currency` (`default_currency`),
  CONSTRAINT `fk_countries_currency` FOREIGN KEY (`default_currency`) REFERENCES `currencies` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `currencies` (
  `code` char(3) NOT NULL,
  `name` varchar(80) NOT NULL,
  `symbol` varchar(8) NOT NULL,
  `decimal_places` tinyint(3) unsigned NOT NULL DEFAULT 2,
  `symbol_position` enum('before','after') NOT NULL DEFAULT 'before',
  `locale` varchar(16) NOT NULL,
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `device_push_tokens` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL,
  `business_id` bigint(20) unsigned NOT NULL,
  `token` varchar(512) NOT NULL,
  `platform` varchar(16) NOT NULL DEFAULT 'android',
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_push_token` (`token`),
  KEY `idx_push_user` (`user_id`),
  KEY `idx_push_biz` (`business_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `payment_gateways` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(40) NOT NULL,
  `name` varchar(80) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT 0,
  `is_default` tinyint(1) NOT NULL DEFAULT 0,
  `supported_countries` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`supported_countries`)),
  `config_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`config_json`)),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_gateway_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `plan_prices` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `plan_id` bigint(20) unsigned NOT NULL,
  `currency_code` char(3) NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `interval_unit` enum('month','year') NOT NULL DEFAULT 'month',
  `interval_count` smallint(5) unsigned NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_plan_price` (`plan_id`,`currency_code`,`interval_unit`,`interval_count`),
  KEY `fk_plan_prices_currency` (`currency_code`),
  CONSTRAINT `fk_plan_prices_currency` FOREIGN KEY (`currency_code`) REFERENCES `currencies` (`code`),
  CONSTRAINT `fk_plan_prices_plan` FOREIGN KEY (`plan_id`) REFERENCES `plans` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `plans` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(40) NOT NULL,
  `name` varchar(80) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `staff_limit` smallint(5) unsigned NOT NULL DEFAULT 0,
  `sort_order` smallint(5) unsigned NOT NULL DEFAULT 0,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_plans_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `product_photos` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` bigint(20) unsigned NOT NULL,
  `path` varchar(255) NOT NULL,
  `sort_order` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_product_photos` (`product_id`),
  CONSTRAINT `fk_product_photos_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `products` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(40) DEFAULT NULL,
  `name` varchar(60) NOT NULL,
  `description` varchar(180) DEFAULT NULL,
  `quantity` int(11) NOT NULL DEFAULT 0,
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  `cover_path` varchar(255) DEFAULT NULL,
  `sort_order` smallint(5) unsigned NOT NULL DEFAULT 0,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_products_business` (`business_id`),
  CONSTRAINT `fk_products_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `promotions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(40) NOT NULL,
  `kind` enum('first_invoice','referral_credit') NOT NULL,
  `currency_code` char(3) NOT NULL,
  `country_code` char(2) NOT NULL DEFAULT '*',
  `amount` decimal(12,2) NOT NULL,
  `campaign` varchar(40) NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `starts_at` datetime DEFAULT NULL,
  `ends_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_promo` (`code`,`kind`,`currency_code`,`country_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `referral_credit_usages` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `credit_id` bigint(20) unsigned NOT NULL,
  `referrer_business_id` bigint(20) unsigned NOT NULL,
  `checkout_session_id` bigint(20) unsigned DEFAULT NULL,
  `subscription_id` bigint(20) unsigned DEFAULT NULL,
  `amount` decimal(12,2) NOT NULL,
  `currency_code` char(3) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_usage_checkout_credit` (`checkout_session_id`,`credit_id`),
  KEY `idx_usage_credit` (`credit_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `referral_credits` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `referrer_business_id` bigint(20) unsigned NOT NULL,
  `referred_business_id` bigint(20) unsigned NOT NULL,
  `referral_id` bigint(20) unsigned NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `currency_code` char(3) NOT NULL,
  `reason` varchar(40) NOT NULL,
  `status` enum('available','applied','void') NOT NULL DEFAULT 'available',
  `campaign` varchar(40) NOT NULL,
  `subscription_id` bigint(20) unsigned DEFAULT NULL,
  `checkout_session_id` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `applied_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_credit_grant` (`referral_id`,`reason`),
  KEY `idx_credit_referrer` (`referrer_business_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `referrals` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `referrer_business_id` bigint(20) unsigned NOT NULL,
  `referred_business_id` bigint(20) unsigned NOT NULL,
  `referral_code` varchar(16) NOT NULL,
  `campaign` varchar(40) NOT NULL,
  `status` enum('pending','converted','rejected','invalid') NOT NULL DEFAULT 'pending',
  `reject_reason` varchar(80) DEFAULT NULL,
  `converted_at` datetime DEFAULT NULL,
  `converting_subscription_id` bigint(20) unsigned DEFAULT NULL,
  `converting_checkout_id` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_referred_business` (`referred_business_id`),
  KEY `idx_referrer` (`referrer_business_id`),
  KEY `idx_ref_code` (`referral_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `service_categories` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(64) NOT NULL,
  `name` varchar(40) NOT NULL,
  `sort_order` smallint(5) unsigned NOT NULL DEFAULT 0,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_svc_cat` (`business_id`,`client_temp_id`),
  CONSTRAINT `fk_svc_cat_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `services` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(40) DEFAULT NULL,
  `name` varchar(50) NOT NULL,
  `duration_minutes` smallint(5) unsigned NOT NULL,
  `price_type` enum('fixo','apartir','variavel') NOT NULL DEFAULT 'fixo',
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  `sort_order` smallint(5) unsigned NOT NULL DEFAULT 0,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `category_temp_id` varchar(64) DEFAULT NULL,
  `color` varchar(16) DEFAULT NULL,
  `description` varchar(300) DEFAULT NULL,
  `bookable` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`),
  KEY `idx_services_business` (`business_id`),
  CONSTRAINT `fk_services_business` FOREIGN KEY (`business_id`) REFERENCES `businesses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `staff` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `client_temp_id` varchar(60) NOT NULL,
  `name` varchar(50) NOT NULL,
  `email` varchar(80) DEFAULT NULL,
  `phone_ddi` varchar(8) NOT NULL DEFAULT '+55',
  `phone_local` varchar(20) DEFAULT NULL,
  `role` enum('admin','pro','front') NOT NULL DEFAULT 'pro',
  `job_title` varchar(40) DEFAULT NULL,
  `description` varchar(280) DEFAULT NULL,
  `photo_path` varchar(255) DEFAULT NULL,
  `invite` tinyint(1) NOT NULL DEFAULT 1,
  `service_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`service_ids`)),
  `same_hours` tinyint(1) NOT NULL DEFAULT 1,
  `hours_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`hours_json`)),
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_staff_client` (`business_id`,`client_temp_id`),
  KEY `idx_staff_business` (`business_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `studio_settings` (
  `k` varchar(80) NOT NULL,
  `v` text DEFAULT NULL,
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`k`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `subscription_events` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `subscription_id` bigint(20) unsigned NOT NULL,
  `business_id` bigint(20) unsigned NOT NULL,
  `from_status` varchar(32) DEFAULT NULL,
  `to_status` varchar(32) NOT NULL,
  `source` varchar(40) NOT NULL,
  `note` varchar(255) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_subev_sub` (`subscription_id`),
  KEY `idx_subev_biz` (`business_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `subscriptions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `business_id` bigint(20) unsigned NOT NULL,
  `plan_id` bigint(20) unsigned NOT NULL,
  `gateway_code` varchar(40) NOT NULL,
  `gateway_customer_id` varchar(120) DEFAULT NULL,
  `gateway_subscription_id` varchar(120) DEFAULT NULL,
  `status` enum('pending','active','past_due','suspended','cancelled') NOT NULL DEFAULT 'pending',
  `currency_code` char(3) NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `interval_unit` enum('month','year') NOT NULL DEFAULT 'month',
  `interval_count` smallint(5) unsigned NOT NULL DEFAULT 1,
  `current_period_start` datetime DEFAULT NULL,
  `current_period_end` datetime DEFAULT NULL,
  `cancel_at_period_end` tinyint(1) NOT NULL DEFAULT 0,
  `canceled_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_sub_business` (`business_id`),
  KEY `idx_sub_gateway` (`gateway_code`,`gateway_subscription_id`),
  KEY `idx_sub_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `training_videos` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(120) NOT NULL,
  `description` varchar(280) DEFAULT NULL,
  `video_path` varchar(512) NOT NULL DEFAULT '',
  `video_kind` enum('file','url','youtube','vimeo') NOT NULL DEFAULT 'file',
  `thumbnail_path` varchar(255) DEFAULT NULL,
  `sort_order` int(11) NOT NULL DEFAULT 0,
  `is_hot` tinyint(1) NOT NULL DEFAULT 1,
  `published` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_tv_pub_sort` (`published`,`is_hot`,`sort_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `user_password_resets` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL,
  `email` varchar(190) NOT NULL,
  `code_hash` varchar(255) NOT NULL,
  `token` char(64) DEFAULT NULL,
  `expires_at` datetime NOT NULL,
  `used_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_recover_user` (`user_id`,`created_at`),
  KEY `idx_recover_token` (`token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(190) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `full_name` varchar(150) NOT NULL,
  `phone_ddi` varchar(8) NOT NULL DEFAULT '+55',
  `phone_local` varchar(20) NOT NULL,
  `phone_e164` varchar(32) NOT NULL,
  `referral_code` varchar(40) DEFAULT NULL,
  `status` enum('pending','active','inactive') NOT NULL DEFAULT 'pending',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `photo_path` varchar(255) DEFAULT NULL,
  `job_title` varchar(40) DEFAULT NULL,
  `description` varchar(280) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_users_email` (`email`),
  KEY `idx_users_phone` (`phone_e164`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `webhook_events` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `gateway_code` varchar(40) NOT NULL,
  `external_id` varchar(120) NOT NULL,
  `event_type` varchar(80) NOT NULL,
  `payload_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`payload_json`)),
  `mapped_status` varchar(32) DEFAULT NULL,
  `subscription_id` bigint(20) unsigned DEFAULT NULL,
  `processed_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_webhook` (`gateway_code`,`external_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET FOREIGN_KEY_CHECKS = 1;
