<?php
/**
 * Dynamic Forms handler.
 *
 * @package Replyan_AI
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Replyan_AI_Forms {

	/**
	 * Create database tables for dynamic forms.
	 */
	public static function create_table(): void {
		global $wpdb;
		$charset_collate = $wpdb->get_charset_collate();

		$table_forms       = $wpdb->prefix . 'replyan_forms';
		$table_fields      = $wpdb->prefix . 'replyan_form_fields';
		$table_submissions = $wpdb->prefix . 'replyan_form_submissions';
		$table_agent_forms = $wpdb->prefix . 'replyan_agent_forms';
		$table_settings    = $wpdb->prefix . 'replyan_form_settings';

		$sql = "CREATE TABLE {$table_forms} (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			name VARCHAR(191) NOT NULL,
			slug VARCHAR(191) NOT NULL,
			description TEXT DEFAULT NULL,
			ai_instruction TEXT DEFAULT NULL,
			button_text VARCHAR(191) DEFAULT NULL,
			success_message TEXT DEFAULT NULL,
			status VARCHAR(20) NOT NULL DEFAULT 'active',
			created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			UNIQUE KEY slug (slug),
			KEY status (status)
		) {$charset_collate};

		CREATE TABLE {$table_fields} (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			form_id BIGINT(20) UNSIGNED NOT NULL,
			label VARCHAR(191) NOT NULL,
			field_key VARCHAR(191) NOT NULL,
			type VARCHAR(50) NOT NULL,
			placeholder VARCHAR(191) DEFAULT NULL,
			help_text TEXT DEFAULT NULL,
			options LONGTEXT DEFAULT NULL,
			is_required TINYINT(1) NOT NULL DEFAULT 0,
			sort_order INT(11) NOT NULL DEFAULT 0,
			validation_rules LONGTEXT DEFAULT NULL,
			ai_extract_hint TEXT DEFAULT NULL,
			status VARCHAR(20) NOT NULL DEFAULT 'active',
			created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY form_id (form_id),
			KEY field_key (field_key),
			KEY sort_order (sort_order)
		) {$charset_collate};

		CREATE TABLE {$table_submissions} (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			form_id BIGINT(20) UNSIGNED NOT NULL,
			session_id VARCHAR(191) DEFAULT NULL,
			user_id BIGINT(20) UNSIGNED DEFAULT NULL,
			agent_id BIGINT(20) UNSIGNED DEFAULT NULL,
			`values` LONGTEXT NOT NULL,
			source VARCHAR(50) NOT NULL DEFAULT 'chat',
			status VARCHAR(20) NOT NULL DEFAULT 'new',
			ip_address VARCHAR(100) DEFAULT NULL,
			user_agent TEXT DEFAULT NULL,
			created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY form_id (form_id),
			KEY session_id (session_id),
			KEY user_id (user_id),
			KEY agent_id (agent_id),
			KEY status (status),
			KEY created_at (created_at)
		) {$charset_collate};

		CREATE TABLE {$table_agent_forms} (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			agent_id BIGINT(20) UNSIGNED NOT NULL,
			form_id BIGINT(20) UNSIGNED NOT NULL,
			is_default TINYINT(1) NOT NULL DEFAULT 0,
			sort_order INT(11) NOT NULL DEFAULT 0,
			created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY agent_id (agent_id),
			KEY form_id (form_id),
			KEY is_default (is_default)
		) {$charset_collate};

		CREATE TABLE {$table_settings} (
			form_id BIGINT(20) UNSIGNED NOT NULL,
			setting_key VARCHAR(50) NOT NULL,
			setting_value LONGTEXT,
			updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (form_id, setting_key)
		) {$charset_collate};";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta( $sql );
	}

	/**
	 * Register hooks.
	 */
	public function register_hooks(): void {
		require_once __DIR__ . '/class-replyan-ai-logic-forms.php';
		( new Replyan_AI_Logic_Forms() )->register_hooks();
	}
}
