-- database/sahibinden_schema.sql

CREATE TABLE IF NOT EXISTS `sahibinden_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mode` enum('csv','cookie','playwright') NOT NULL DEFAULT 'csv',
  `store_url` varchar(255) DEFAULT NULL,
  `encrypted_cookie` text DEFAULT NULL,
  `cookie_iv` varchar(100) DEFAULT NULL,
  `fallback_enabled` tinyint(1) NOT NULL DEFAULT 1,
  `cookie_expires_at` timestamp NULL DEFAULT NULL,
  `last_check` timestamp NULL DEFAULT NULL,
  `last_error` text DEFAULT NULL,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `sahibinden_logs` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `mode_used` enum('csv','cookie','playwright') NOT NULL,
  `status` enum('success','error','fallback_triggered') NOT NULL,
  `products_checked` int(11) DEFAULT 0,
  `products_updated` int(11) DEFAULT 0,
  `message` text NOT NULL, -- Masked/Sanitized messages only
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Insert default settings
INSERT IGNORE INTO `sahibinden_settings` (`id`, `mode`, `store_url`) VALUES (1, 'csv', 'https://mobileks.sahibinden.com');
