-- Table to store Agristack Farmer Verification requests
CREATE TABLE IF NOT EXISTS `agristack_farmer` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `application_no` VARCHAR(100) NOT NULL,
  `username` VARCHAR(20) NOT NULL,
  `aadhaar` VARCHAR(15) NOT NULL,
  `state` VARCHAR(5) NOT NULL,
  `name` VARCHAR(150) DEFAULT NULL,
  `data_mode` VARCHAR(20) DEFAULT NULL,
  `source` VARCHAR(20) DEFAULT NULL,
  `billable` VARCHAR(1) DEFAULT 'Y' COMMENT 'Y = charged, N = free (cached hit)',
  `pdf_base64` LONGTEXT COMMENT 'raw base64 PDF data from API, rendered client-side via Blob URL',
  `status` TINYINT(1) NOT NULL DEFAULT 0,
  `status_code` TEXT,
  `fee` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT 'actual amount charged (0 if billable=N)',
  `old_balance` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `new_balance` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `message` VARCHAR(255) DEFAULT NULL,
  `date` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_username` (`username`),
  KEY `idx_application_no` (`application_no`),
  KEY `idx_aadhaar` (`aadhaar`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Add pricing entry (adjust price as needed) - this is the MAX charge; actual charge is waived when API returns billable=N
INSERT INTO `pricing` (`service_name`, `price`) VALUES ('Agristack_farmer', 15)
ON DUPLICATE KEY UPDATE price = price;
