-- Reseller API System
-- Adds api_key column to users table and api_logs table

-- Add API key to users
ALTER TABLE `users`
ADD COLUMN `api_key` VARCHAR(64) DEFAULT NULL COMMENT 'Reseller API key',
ADD COLUMN `api_enabled` TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Whether API access is enabled',
ADD UNIQUE INDEX `idx_api_key` (`api_key`);

-- API request logs
CREATE TABLE IF NOT EXISTS `api_logs` (
  `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `user_id` INT NOT NULL,
  `endpoint` VARCHAR(100) NOT NULL,
  `method` VARCHAR(10) NOT NULL DEFAULT 'GET',
  `ip_address` VARCHAR(45) NOT NULL,
  `request_data` TEXT DEFAULT NULL,
  `response_status` VARCHAR(10) DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX `idx_user_id` (`user_id`),
  INDEX `idx_created_at` (`created_at`),
  INDEX `idx_endpoint` (`endpoint`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Add API markup setting
ALTER TABLE `settings`
ADD COLUMN `api_markup` DECIMAL(5,2) NOT NULL DEFAULT 0.00 COMMENT 'Extra markup % for API purchases',
ADD COLUMN `api_enabled_global` TINYINT(1) NOT NULL DEFAULT 1 COMMENT 'Global API on/off switch';
