-- Coconut Trade Fair — canonical single-event production schema
--
-- Import this file into the already-selected database from cPanel/phpMyAdmin.
-- It intentionally does not create or select a database, drop objects, add
-- foreign keys, or seed operational/staff data. Sequelize model definitions
-- remain the source of truth for the table structures below.
--
-- Re-importing this file is non-destructive. The seven static registration
-- fields use fieldKey as their unique synchronization key.

SET NAMES utf8mb4;

-- --------------------------------------------------------
-- registration_fields — dynamic registration form configuration
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `registration_fields` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `fieldKey` VARCHAR(100) NOT NULL,
  `label` TEXT NOT NULL,
  `fieldType` ENUM('text', 'radio', 'checkbox') NOT NULL,
  `fieldRole` ENUM(
    'none',
    'fullName',
    'firstName',
    'lastName',
    'email',
    'mobileNumber',
    'company',
    'position'
  ) NOT NULL DEFAULT 'none',
  `options` JSON NULL,
  `validation` JSON NULL,
  `placeholder` VARCHAR(255) NULL,
  `helpText` VARCHAR(255) NULL,
  `displayOrder` INTEGER NOT NULL DEFAULT 0,
  `active` TINYINT(1) NOT NULL DEFAULT 1,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `fieldKey` (`fieldKey`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- registrants — one row per registration
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `registrants` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `guid` CHAR(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  `responses` JSON NULL,
  `fullName` VARCHAR(255) NULL,
  `email` VARCHAR(255) NULL,
  `normalizedEmail` VARCHAR(255) NULL,
  `mobileNumber` VARCHAR(255) NULL,
  `company` VARCHAR(255) NULL,
  `source` VARCHAR(50) NOT NULL DEFAULT 'direct',
  `active` INTEGER NOT NULL DEFAULT 1,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `guid` (`guid`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- registration_field_answers — dual-written readable answers
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `registration_field_answers` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `registrantId` BIGINT NOT NULL,
  `registrationFieldId` BIGINT NULL,
  `fieldLabel` TEXT NOT NULL,
  `fieldType` VARCHAR(20) NOT NULL,
  `answerValue` TEXT NULL,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- registration_qr — QR emailed after registration
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `registration_qr` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `registrantGuid` CHAR(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  `hashedId` VARCHAR(255) NOT NULL,
  `ticketId` VARCHAR(30) NULL,
  `printedAt` DATETIME NULL,
  `printed_by` BIGINT NULL,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `registrantGuid` (`registrantGuid`),
  UNIQUE KEY `hashedId` (`hashedId`),
  UNIQUE KEY `ticketId` (`ticketId`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- badge_qr — QR printed on the badge and scanned at entrance
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `badge_qr` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `registrantGuid` CHAR(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  `referenceNumber` VARCHAR(50) NOT NULL,
  `qrUrl` VARCHAR(500) NOT NULL,
  `dateAttended` DATETIME NULL,
  `scanned_by` BIGINT NULL,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `registrantGuid` (`registrantGuid`),
  UNIQUE KEY `referenceNumber` (`referenceNumber`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- scanner_users — staff accounts; intentionally no seed rows
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `scanner_users` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(100) NOT NULL,
  `password_hash` VARCHAR(255) NOT NULL,
  `role` ENUM('scanner', 'badge_printer', 'admin') NOT NULL DEFAULT 'scanner',
  `full_name` VARCHAR(255) NULL,
  `counter_number` VARCHAR(50) NULL,
  `active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- email_logs — truthful registration QR delivery attempts
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `email_logs` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `registrantGuid` CHAR(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL,
  `emailType` ENUM('qr_code') NOT NULL DEFAULT 'qr_code',
  `recipientName` VARCHAR(255) NULL,
  `recipientEmail` VARCHAR(255) NULL,
  `subject` VARCHAR(255) NULL,
  `ticketId` VARCHAR(100) NULL,
  `qrCodeImage` LONGTEXT NULL,
  `status` ENUM('sent', 'failed') NOT NULL DEFAULT 'sent',
  `errorMessage` TEXT NULL,
  `sentAt` DATETIME NULL,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------
-- Current CTF registration form (safe static configuration only)
-- --------------------------------------------------------
INSERT INTO `registration_fields`
  (`fieldKey`, `label`, `fieldType`, `fieldRole`, `options`, `validation`,
   `placeholder`, `helpText`, `displayOrder`, `active`, `createdAt`, `updatedAt`)
VALUES
  (
    'first_name',
    'First Name',
    'text',
    'firstName',
    NULL,
    '{"required":true}',
    'Enter Firstname',
    NULL,
    1,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  ),
  (
    'last_name',
    'Last Name',
    'text',
    'lastName',
    NULL,
    NULL,
    'Enter Lastname',
    NULL,
    2,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  ),
  (
    'email',
    'email',
    'text',
    'email',
    NULL,
    '{"required":true,"pattern":"^[^@\\\\s]+@[^@\\\\s]+\\\\.[^@\\\\s]+$"}',
    'Enter Email Address',
    NULL,
    2,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  ),
  (
    'mobile_number',
    'Mobile Number',
    'text',
    'mobileNumber',
    NULL,
    '{"required":true,"minLength":10,"maxLength":10,"pattern":"^9[0-9]{9}$"}',
    'Enter mobile number',
    NULL,
    4,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  ),
  (
    'company',
    'Company',
    'text',
    'company',
    NULL,
    '{"required":true}',
    'Enter your company',
    NULL,
    3,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  ),
  (
    'purpose_of_visit',
    'Purpose of Visit',
    'radio',
    'none',
    '[{"label":"Make a purchase or place booked orders","value":"Make a purchase or place booked orders"},{"label":"Meet existing suppliers","value":"Meet existing suppliers"},{"label":"Meet new suppliers","value":"Meet new suppliers"},{"label":"See new products","value":"See new product"},{"label":"Update business contacts","value":"Update business contacts"},{"label":"Evaluate the event for particapation","value":"Evaluate the event for particapation"},{"label":"Others","value":"Others"}]',
    '{"required":true}',
    NULL,
    NULL,
    5,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  ),
  (
    'position',
    'Job Position',
    'text',
    'position',
    NULL,
    NULL,
    'Enter your job position',
    NULL,
    6,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  )
ON DUPLICATE KEY UPDATE
  `label` = VALUES(`label`),
  `fieldType` = VALUES(`fieldType`),
  `fieldRole` = VALUES(`fieldRole`),
  `options` = VALUES(`options`),
  `validation` = VALUES(`validation`),
  `placeholder` = VALUES(`placeholder`),
  `helpText` = VALUES(`helpText`),
  `displayOrder` = VALUES(`displayOrder`),
  `active` = VALUES(`active`),
  `updatedAt` = CURRENT_TIMESTAMP;
