-- OTP Assist Portal | Limit Cross: install once, non-destructive, MariaDB 10.3+
-- BACK UP LIVE DB FIRST. Does not DROP or ALTER existing tables.
CREATE TABLE IF NOT EXISTS `limit_cross_cases` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `applied_by` VARCHAR(50) NOT NULL,
  `parent_userid` VARCHAR(50) DEFAULT NULL,
  `submitted_role` VARCHAR(30) NOT NULL,
  `correction_type` ENUM('dob','name') NOT NULL,
  `document_mode` ENUM('with_document','without_document') NOT NULL,
  `customer_name` VARCHAR(150) NOT NULL,
  `aadhar_no` VARCHAR(12) NOT NULL,
  `mobile_no` VARCHAR(15) NOT NULL,
  `old_name` VARCHAR(150) DEFAULT NULL,
  `new_name` VARCHAR(150) DEFAULT NULL,
  `new_name_local` VARCHAR(255) DEFAULT NULL,
  `old_dob` DATE DEFAULT NULL,
  `new_dob` DATE DEFAULT NULL,
  `gender` VARCHAR(20) NOT NULL,
  `father_name` VARCHAR(150) NOT NULL,
  `mother_name` VARCHAR(150) NOT NULL,
  `full_address` TEXT NOT NULL,
  `state` VARCHAR(100) NOT NULL,
  `photo_file` VARCHAR(500) NOT NULL,
  `self_declaration_file` VARCHAR(500) DEFAULT NULL,
  `birth_certificate_file` VARCHAR(500) DEFAULT NULL,
  `aadhaar_copy_file` VARCHAR(500) DEFAULT NULL,
  `iris_file_1` VARCHAR(500) NOT NULL,
  `iris_file_2` VARCHAR(500) NOT NULL,
  `iris_file_3` VARCHAR(500) NOT NULL,
  `iris_file_4` VARCHAR(500) NOT NULL,
  `iris_file_5` VARCHAR(500) NOT NULL,
  `deducted_amount` DECIMAL(14,2) NOT NULL,
  `commission_amount` DECIMAL(14,2) NOT NULL DEFAULT 0.00,
  `payment_reference` VARCHAR(150) NOT NULL,
  `request_token` VARCHAR(64) NOT NULL,
  `status` ENUM('Pending','Success','Reject') NOT NULL DEFAULT 'Pending',
  `remark` VARCHAR(500) DEFAULT NULL,
  `assigned_to` VARCHAR(50) DEFAULT NULL,
  `assigned_at` DATETIME DEFAULT NULL,
  `assigned_by` VARCHAR(50) DEFAULT NULL,
  `completed_at` DATETIME DEFAULT NULL,
  `completed_by` VARCHAR(50) DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_lc_payment` (`payment_reference`),
  UNIQUE KEY `uq_lc_request` (`request_token`),
  KEY `idx_lc_owner_date` (`applied_by`,`created_at`),
  KEY `idx_lc_queue` (`status`,`assigned_to`,`created_at`),
  KEY `idx_lc_type` (`correction_type`,`document_mode`,`created_at`),
  KEY `idx_lc_parent` (`parent_userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `limit_cross_events` (
 `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
 `case_id` BIGINT UNSIGNED NOT NULL,
 `actor_userid` VARCHAR(50) NOT NULL,
 `action_type` VARCHAR(40) NOT NULL,
 `old_value` VARCHAR(255) DEFAULT NULL,
 `new_value` VARCHAR(255) DEFAULT NULL,
 `remark` VARCHAR(500) DEFAULT NULL,
 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`), KEY `idx_lc_events` (`case_id`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `limit_cross_refunds` (
 `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
 `case_id` BIGINT UNSIGNED NOT NULL,
 `userid` VARCHAR(50) NOT NULL,
 `refunded_amount` DECIMAL(14,2) NOT NULL,
 `refund_reference` VARCHAR(150) NOT NULL,
 `approved_by` VARCHAR(50) NOT NULL,
 `approved_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`), UNIQUE KEY `uq_lc_ref_case` (`case_id`),
 UNIQUE KEY `uq_lc_ref_reference` (`refund_reference`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Use the existing service_settings table; INSERT IGNORE preserves edits on re-install.
-- Distributor rate defaults to same as Retailer; commission defaults to zero (not specified).
INSERT IGNORE INTO `service_settings`
 (`service_key`,`service_name`,`retailer_charge`,`distributor_charge`,`distributor_commission`,`refund_on_reject`,`is_active`) VALUES
 ('lc_dob_with_document','Limit Cross DOB With Document',8000.00,8000.00,0.00,0,1),
 ('lc_dob_without_document','Limit Cross DOB Without Document',10000.00,10000.00,0.00,0,1),
 ('lc_name_with_document','Limit Cross Name With Document',8000.00,8000.00,0.00,0,1),
 ('lc_name_without_document','Limit Cross Name Without Document',10000.00,10000.00,0.00,0,1);

-- Enforce the agreed identical charges for retailer and distributor.
UPDATE service_settings SET retailer_charge=8000.00,distributor_charge=8000.00,is_active=1 WHERE service_key IN ('lc_dob_with_document','lc_name_with_document');
UPDATE service_settings SET retailer_charge=10000.00,distributor_charge=10000.00,is_active=1 WHERE service_key IN ('lc_dob_without_document','lc_name_without_document');
-- DWG operator role already exists in the portal; no new role or user migration.
