-- ============================================================================
-- CT NAILS — Premium Nail Studio
-- Phase 1: Database Schema (MySQL 8 / MariaDB 10.6+)
-- Engine: InnoDB | Charset: utf8mb4 | Collation: utf8mb4_unicode_ci
--
-- Import:
--   mysql -u <user> -p <database> < database/schema.sql
--   mysql -u <user> -p <database> < database/seed.sql
--
-- Notes:
--  * All money is DECIMAL(10,2) in LKR.
--  * Double-booking is prevented at DB level by the UNIQUE key
--    uq_staff_slot on appointments (staff_id, appointment_date, start_time).
--    The booking code MUST also re-check the slot inside a transaction
--    before INSERT (Phase 4) — the UNIQUE key is the last line of defence.
--  * MySQL UNIQUE keys treat NULL staff_id as distinct, so a booking with
--    no staff assigned cannot collide with a staffed booking. When a staff
--    member is assigned later, the app must re-check availability.
-- ============================================================================

SET NAMES utf8mb4;
SET time_zone = '+00:00';
SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- ----------------------------------------------------------------------------
-- 1. users — customers (customer portal accounts)
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`          VARCHAR(100)    NOT NULL,
  `email`         VARCHAR(191)    NOT NULL,
  `phone`         VARCHAR(20)     NOT NULL,
  `password_hash` VARCHAR(255)    NOT NULL,
  `birthday`      DATE            NULL DEFAULT NULL,
  `role`          ENUM('customer') NOT NULL DEFAULT 'customer',
  `status`        ENUM('active','inactive','banned') NOT NULL DEFAULT 'active',
  `points_balance` INT            NOT NULL DEFAULT 0,
  `email_verified_at` TIMESTAMP NULL DEFAULT NULL,
  `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 `uq_users_email` (`email`),
  INDEX `idx_users_status` (`status`),
  INDEX `idx_users_phone` (`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Customer accounts for the customer portal';

-- ----------------------------------------------------------------------------
-- 2. admins — back-office accounts (separate from customers, per brief)
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `admins` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`          VARCHAR(100)    NOT NULL,
  `email`         VARCHAR(191)    NOT NULL,
  `password_hash` VARCHAR(255)    NOT NULL,
  `role`          ENUM('super_admin','admin') NOT NULL DEFAULT 'admin',
  `status`        ENUM('active','inactive') NOT NULL DEFAULT 'active',
  `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 `uq_admins_email` (`email`),
  INDEX `idx_admins_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Admin dashboard accounts';

-- ----------------------------------------------------------------------------
-- 3. staff — nail technicians / artists
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `staff` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`        VARCHAR(100)    NOT NULL,
  `email`       VARCHAR(191)    NULL DEFAULT NULL,
  `phone`       VARCHAR(20)     NULL DEFAULT NULL,
  `position`    VARCHAR(100)    NOT NULL DEFAULT 'Nail Technician',
  `bio`         TEXT            NULL DEFAULT NULL,
  `specialties` VARCHAR(255)    NULL DEFAULT NULL COMMENT 'Comma-separated, e.g. Gel, Nail Art',
  `photo`       VARCHAR(255)    NULL DEFAULT NULL COMMENT 'Relative path under uploads/staff/',
  `status`      ENUM('active','inactive','on_leave') NOT NULL DEFAULT 'active',
  `sort_order`  INT             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`),
  UNIQUE KEY `uq_staff_email` (`email`),
  INDEX `idx_staff_status` (`status`),
  INDEX `idx_staff_sort` (`sort_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Salon staff / nail artists';

-- ----------------------------------------------------------------------------
-- 4. service_categories
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `service_categories` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`        VARCHAR(100)    NOT NULL,
  `slug`        VARCHAR(120)    NOT NULL,
  `description` TEXT            NULL DEFAULT NULL,
  `image`       VARCHAR(255)    NULL DEFAULT NULL,
  `sort_order`  INT             NOT NULL DEFAULT 0,
  `is_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 `uq_categories_slug` (`slug`),
  UNIQUE KEY `uq_categories_name` (`name`),
  INDEX `idx_categories_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Service categories: Gel, Acrylic, Nail Art, Press-on, Custom';

-- ----------------------------------------------------------------------------
-- 5. services
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `services` (
  `id`               BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `category_id`      BIGINT UNSIGNED NOT NULL,
  `name`             VARCHAR(150)    NOT NULL,
  `slug`             VARCHAR(180)    NOT NULL,
  `description`      TEXT            NULL DEFAULT NULL,
  `price`            DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `duration_minutes` INT             NOT NULL DEFAULT 60,
  `image`            VARCHAR(255)    NULL DEFAULT NULL COMMENT 'Relative path under uploads/services/',
  `is_featured`      TINYINT(1)      NOT NULL DEFAULT 0,
  `is_active`        TINYINT(1)      NOT NULL DEFAULT 1,
  `sort_order`       INT             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`),
  UNIQUE KEY `uq_services_slug` (`slug`),
  INDEX `idx_services_category` (`category_id`),
  INDEX `idx_services_active` (`is_active`),
  INDEX `idx_services_featured` (`is_featured`),
  CONSTRAINT `fk_services_category`
    FOREIGN KEY (`category_id`) REFERENCES `service_categories` (`id`)
    ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Bookable services with price and duration';

-- ----------------------------------------------------------------------------
-- 6. appointments — the core booking record
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `appointments` (
  `id`              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `booking_ref`     VARCHAR(20)     NOT NULL COMMENT 'e.g. CT-20261201-0001',
  `user_id`         BIGINT UNSIGNED NULL DEFAULT NULL COMMENT 'NULL = guest booking',
  `staff_id`        BIGINT UNSIGNED NULL DEFAULT NULL,
  `customer_name`   VARCHAR(100)    NOT NULL,
  `customer_phone`  VARCHAR(20)     NOT NULL,
  `customer_email`  VARCHAR(191)    NULL DEFAULT NULL,
  `appointment_date` DATE           NOT NULL,
  `start_time`      TIME            NOT NULL,
  `end_time`        TIME            NOT NULL,
  `status`          ENUM('pending','confirmed','completed','cancelled','rejected','no_show')
                    NOT NULL DEFAULT 'pending',
  `total_amount`    DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `deposit_amount`  DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `discount_amount` DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `payment_status`  ENUM('unpaid','deposit_paid','paid','refunded')
                    NOT NULL DEFAULT 'unpaid',
  `coupon_code`     VARCHAR(50)     NULL DEFAULT NULL,
  `notes`           TEXT            NULL DEFAULT NULL,
  `cancel_reason`   VARCHAR(255)    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 `uq_appointments_ref` (`booking_ref`),
  -- DB-level double-booking protection for staffed bookings.
  UNIQUE KEY `uq_staff_slot` (`staff_id`, `appointment_date`, `start_time`),
  INDEX `idx_appt_user` (`user_id`),
  INDEX `idx_appt_staff` (`staff_id`),
  INDEX `idx_appt_date` (`appointment_date`),
  INDEX `idx_appt_status` (`status`),
  INDEX `idx_appt_date_status` (`appointment_date`, `status`),
  CONSTRAINT `fk_appt_user`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL,
  CONSTRAINT `fk_appt_staff`
    FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Appointments. Cancelled/rejected rows keep their slot in history; the app must exclude cancelled/rejected rows when checking availability, or delete the slot. See Phase 4 booking logic.';

-- ----------------------------------------------------------------------------
-- 7. appointment_services — services inside one appointment (many-to-many)
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `appointment_services` (
  `id`                BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `appointment_id`    BIGINT UNSIGNED NOT NULL,
  `service_id`        BIGINT UNSIGNED NOT NULL,
  `price_at_booking`  DECIMAL(10,2)   NOT NULL DEFAULT 0.00 COMMENT 'Snapshot of service price at booking time',
  `quantity`          INT             NOT NULL DEFAULT 1,
  `created_at`        TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_apptsvc_appt` (`appointment_id`),
  INDEX `idx_apptsvc_service` (`service_id`),
  CONSTRAINT `fk_apptsvc_appt`
    FOREIGN KEY (`appointment_id`) REFERENCES `appointments` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_apptsvc_service`
    FOREIGN KEY (`service_id`) REFERENCES `services` (`id`)
    ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 8. availability — weekly working hours (staff-specific or salon-wide)
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `availability` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `staff_id`     BIGINT UNSIGNED NULL DEFAULT NULL COMMENT 'NULL = salon-wide opening hours',
  `day_of_week`  TINYINT UNSIGNED NOT NULL COMMENT '0=Sunday .. 6=Saturday',
  `start_time`   TIME            NOT NULL,
  `end_time`     TIME            NOT NULL,
  `is_available` 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 `uq_availability` (`staff_id`, `day_of_week`),
  INDEX `idx_avail_day` (`day_of_week`),
  CONSTRAINT `fk_avail_staff`
    FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `chk_avail_day` CHECK (`day_of_week` BETWEEN 0 AND 6)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Weekly availability. NULL staff_id = salon opening hours.';

-- ----------------------------------------------------------------------------
-- 9. blocked_slots — holidays / blocked dates and times
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `blocked_slots` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `staff_id`     BIGINT UNSIGNED NULL DEFAULT NULL COMMENT 'NULL = whole salon closed',
  `blocked_date` DATE            NOT NULL,
  `start_time`   TIME            NULL DEFAULT NULL COMMENT 'NULL = whole day blocked',
  `end_time`     TIME            NULL DEFAULT NULL,
  `reason`       VARCHAR(255)    NOT NULL DEFAULT 'Closed',
  `created_by`   BIGINT UNSIGNED NULL DEFAULT NULL,
  `created_at`   TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_blocked_date` (`blocked_date`),
  INDEX `idx_blocked_staff_date` (`staff_id`, `blocked_date`),
  CONSTRAINT `fk_blocked_staff`
    FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_blocked_admin`
    FOREIGN KEY (`created_by`) REFERENCES `admins` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 10. payments — PayHere / cash / deposit records
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `payments` (
  `id`                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `appointment_id`     BIGINT UNSIGNED NOT NULL,
  `user_id`            BIGINT UNSIGNED NULL DEFAULT NULL,
  `transaction_id`     VARCHAR(100)    NULL DEFAULT NULL COMMENT 'Internal receipt / transaction reference',
  `payment_method`     ENUM('payhere','cash','deposit','full') NOT NULL DEFAULT 'cash',
  `amount`             DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `currency`           CHAR(3)         NOT NULL DEFAULT 'LKR',
  `status`             ENUM('pending','paid','failed','refunded') NOT NULL DEFAULT 'pending',
  `payhere_order_id`   VARCHAR(100)    NULL DEFAULT NULL,
  `payhere_payment_id` VARCHAR(100)    NULL DEFAULT NULL,
  `receipt_no`         VARCHAR(50)     NULL DEFAULT NULL,
  `notes`              TEXT            NULL DEFAULT NULL,
  `paid_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 `uq_payments_txn` (`transaction_id`),
  INDEX `idx_payments_appt` (`appointment_id`),
  INDEX `idx_payments_user` (`user_id`),
  INDEX `idx_payments_status` (`status`),
  INDEX `idx_payments_payhere` (`payhere_payment_id`),
  CONSTRAINT `fk_payments_appt`
    FOREIGN KEY (`appointment_id`) REFERENCES `appointments` (`id`)
    ON UPDATE CASCADE ON DELETE RESTRICT,
  CONSTRAINT `fk_payments_user`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Payments. A payment is marked paid ONLY after server-side PayHere verification (Phase 6). Never mark paid from client-side data.';

-- ----------------------------------------------------------------------------
-- 11. memberships — Silver / Gold / Platinum tiers
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `memberships` (
  `id`                BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `tier`              ENUM('silver','gold','platinum') NOT NULL,
  `name`              VARCHAR(50)     NOT NULL,
  `discount_percent`  DECIMAL(5,2)    NOT NULL DEFAULT 0.00,
  `price`             DECIMAL(10,2)   NOT NULL DEFAULT 0.00 COMMENT '0 = earned by points/spend, not purchased',
  `points_multiplier` DECIMAL(3,2)    NOT NULL DEFAULT 1.00,
  `benefits`          JSON            NULL DEFAULT NULL COMMENT 'JSON array of benefit strings',
  `is_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 `uq_memberships_tier` (`tier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 12. customer_memberships — which tier a customer currently holds
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `customer_memberships` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`       BIGINT UNSIGNED NOT NULL,
  `membership_id` BIGINT UNSIGNED NOT NULL,
  `started_at`    TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `expires_at`    TIMESTAMP   NULL DEFAULT NULL,
  `status`        ENUM('active','expired','cancelled') NOT NULL DEFAULT 'active',
  `created_at`    TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`    TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_custmemb_user` (`user_id`),
  INDEX `idx_custmemb_status` (`status`),
  INDEX `idx_custmemb_user_status` (`user_id`, `status`),
  CONSTRAINT `fk_custmemb_user`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_custmemb_membership`
    FOREIGN KEY (`membership_id`) REFERENCES `memberships` (`id`)
    ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='One active membership per customer is enforced in application code (Phase 7); history rows are kept with status expired/cancelled.';

-- ----------------------------------------------------------------------------
-- 13. loyalty_points — points ledger (earn / redeem / adjust)
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `loyalty_points` (
  `id`             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`        BIGINT UNSIGNED NOT NULL,
  `appointment_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `points`         INT             NOT NULL COMMENT 'Positive = earned, negative = redeemed/adjusted down',
  `transaction_type` ENUM('earn','redeem','adjust','birthday') NOT NULL DEFAULT 'earn',
  `balance_after`  INT             NOT NULL DEFAULT 0,
  `description`    VARCHAR(255)    NULL DEFAULT NULL,
  `created_at`     TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_loyalty_user` (`user_id`),
  INDEX `idx_loyalty_appt` (`appointment_id`),
  INDEX `idx_loyalty_type` (`transaction_type`),
  CONSTRAINT `fk_loyalty_user`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_loyalty_appt`
    FOREIGN KEY (`appointment_id`) REFERENCES `appointments` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Append-only points ledger. users.points_balance is a cached total; the ledger is the source of truth.';

-- ----------------------------------------------------------------------------
-- 14. coupons
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `coupons` (
  `id`             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`           VARCHAR(50)     NOT NULL,
  `discount_type`  ENUM('percent','fixed') NOT NULL DEFAULT 'percent',
  `discount_value` DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `min_amount`     DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `valid_from`     DATE            NULL DEFAULT NULL,
  `valid_to`       DATE            NULL DEFAULT NULL,
  `usage_limit`    INT             NULL DEFAULT NULL,
  `used_count`     INT             NOT NULL DEFAULT 0,
  `is_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 `uq_coupons_code` (`code`),
  INDEX `idx_coupons_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 15. gallery — images and videos, admin-managed
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `gallery` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title`       VARCHAR(150)    NOT NULL,
  `description` TEXT            NULL DEFAULT NULL,
  `media_type`  ENUM('image','video') NOT NULL DEFAULT 'image',
  `file_path`   VARCHAR(255)    NOT NULL COMMENT 'Relative path under uploads/gallery/',
  `thumbnail`   VARCHAR(255)    NULL DEFAULT NULL,
  `category`    VARCHAR(100)    NOT NULL DEFAULT 'New Designs',
  `is_featured` TINYINT(1)      NOT NULL DEFAULT 0,
  `sort_order`  INT             NOT NULL DEFAULT 0,
  `is_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`),
  INDEX `idx_gallery_category` (`category`),
  INDEX `idx_gallery_featured` (`is_featured`),
  INDEX `idx_gallery_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 16. reviews
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `reviews` (
  `id`             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`        BIGINT UNSIGNED NOT NULL,
  `appointment_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `service_id`     BIGINT UNSIGNED NULL DEFAULT NULL,
  `rating`         TINYINT UNSIGNED NOT NULL COMMENT '1-5',
  `comment`        TEXT            NULL DEFAULT NULL,
  `status`         ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  `is_featured`    TINYINT(1)      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`),
  INDEX `idx_reviews_user` (`user_id`),
  INDEX `idx_reviews_service` (`service_id`),
  INDEX `idx_reviews_status` (`status`),
  CONSTRAINT `fk_reviews_user`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_reviews_appt`
    FOREIGN KEY (`appointment_id`) REFERENCES `appointments` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL,
  CONSTRAINT `fk_reviews_service`
    FOREIGN KEY (`service_id`) REFERENCES `services` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL,
  CONSTRAINT `chk_reviews_rating` CHECK (`rating` BETWEEN 1 AND 5)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Reviews are shown publicly only when status = approved.';

-- ----------------------------------------------------------------------------
-- 17. notifications — email / website / WhatsApp-ready records
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `notifications` (
  `id`                     BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`                BIGINT UNSIGNED NULL DEFAULT NULL COMMENT 'NULL = broadcast / admin notice',
  `type`                   VARCHAR(50)     NOT NULL COMMENT 'booking_created, booking_approved, payment_successful, membership_upgraded, points_earned, offer, ...',
  `title`                  VARCHAR(150)    NOT NULL,
  `message`                TEXT            NOT NULL,
  `channel`                ENUM('email','website','whatsapp') NOT NULL DEFAULT 'website',
  `is_read`                TINYINT(1)      NOT NULL DEFAULT 0,
  `related_appointment_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `sent_at`                TIMESTAMP   NULL DEFAULT NULL,
  `created_at`             TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_notif_user` (`user_id`),
  INDEX `idx_notif_type` (`type`),
  INDEX `idx_notif_read` (`user_id`, `is_read`),
  CONSTRAINT `fk_notif_user`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_notif_appt`
    FOREIGN KEY (`related_appointment_id`) REFERENCES `appointments` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 18. settings — salon info, opening hours, site content (key/value)
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `settings` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `setting_key`   VARCHAR(100)    NOT NULL,
  `setting_value` TEXT            NULL DEFAULT NULL,
  `description`   VARCHAR(255)    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 `uq_settings_key` (`setting_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------------------------------------------------------
-- 19. audit_logs — admin actions, for security and accountability
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `audit_logs` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `admin_id`    BIGINT UNSIGNED NULL DEFAULT NULL,
  `action`      VARCHAR(100)    NOT NULL COMMENT 'e.g. appointment.approve, service.update, settings.change',
  `entity_type` VARCHAR(50)     NULL DEFAULT NULL,
  `entity_id`   BIGINT UNSIGNED NULL DEFAULT NULL,
  `details`     JSON            NULL DEFAULT NULL,
  `ip_address`  VARCHAR(45)     NULL DEFAULT NULL,
  `created_at`  TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  INDEX `idx_audit_admin` (`admin_id`),
  INDEX `idx_audit_action` (`action`),
  INDEX `idx_audit_created` (`created_at`),
  CONSTRAINT `fk_audit_admin`
    FOREIGN KEY (`admin_id`) REFERENCES `admins` (`id`)
    ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================================================
-- End of schema — 19 tables.
-- Next: import database/seed.sql
-- ============================================================================
