-- SQL to create WP Agentic Writer tables
-- Run this in phpMyAdmin or Local's Adminer tool

-- Cost tracking table
CREATE TABLE IF NOT EXISTS `wp_wpaw_cost_tracking` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) NOT NULL,
  `model` varchar(255) NOT NULL,
  `action` varchar(50) NOT NULL,
  `input_tokens` int(11) NOT NULL,
  `output_tokens` int(11) NOT NULL,
  `cost` decimal(10,6) NOT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
  PRIMARY KEY (`id`),
  KEY `post_id` (`post_id`),
  KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Image recommendations table
CREATE TABLE IF NOT EXISTS `wp_wpaw_images` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) NOT NULL,
  `agent_image_id` varchar(50) NOT NULL,
  `placement` varchar(100) DEFAULT NULL,
  `section_title` varchar(255) DEFAULT NULL,
  `prompt_initial` text NOT NULL,
  `alt_text_initial` text DEFAULT NULL,
  `prompt_edited` text DEFAULT NULL,
  `alt_text_edited` text DEFAULT NULL,
  `attachment_id` bigint(20) DEFAULT NULL,
  `status` varchar(30) DEFAULT 'pending',
  `cost_estimate` decimal(10, 4) DEFAULT NULL,
  `cost_actual` decimal(10, 4) DEFAULT NULL,
  `image_model` varchar(100) DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_post` (`post_id`),
  KEY `idx_agent_image_id` (`post_id`, `agent_image_id`),
  KEY `idx_status` (`status`),
  KEY `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Image variants table
CREATE TABLE IF NOT EXISTS `wp_wpaw_images_variants` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `agentic_image_id` bigint(20) NOT NULL,
  `post_id` bigint(20) NOT NULL,
  `agent_image_id` varchar(50) NOT NULL,
  `variant_number` int(11) DEFAULT 1,
  `temp_file_path` varchar(500) NOT NULL,
  `temp_file_url` varchar(500) NOT NULL,
  `file_size` int(11) DEFAULT NULL,
  `prompt_used` text DEFAULT NULL,
  `image_model_used` varchar(100) DEFAULT NULL,
  `generation_time` int(11) DEFAULT NULL,
  `cost` decimal(10, 4) DEFAULT NULL,
  `is_selected` tinyint(1) DEFAULT 0,
  `selected_at` datetime DEFAULT NULL,
  `status` varchar(30) DEFAULT 'temp',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
  `deleted_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_agentic_image` (`agentic_image_id`),
  KEY `idx_post` (`post_id`),
  KEY `idx_status` (`status`),
  KEY `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Verify tables were created
SHOW TABLES LIKE 'wp_wpaw_%';

-- Show table structures
DESCRIBE wp_wpaw_cost_tracking;
DESCRIBE wp_wpaw_images;
DESCRIBE wp_wpaw_images_variants;
