<?php
/**
 * Database Migration Class
 *
 * Handles creation and management of database tables for forms and responses.
 *
 * @package PCFormBuilder
 */

class PC_DB_Migration {

    private static $charset_collate = '';

    public static function activate() {
        self::set_charset();
        self::create_forms_table();
        self::create_fields_table();
        self::create_responses_table();
        self::create_response_data_table();

        update_option( 'pcfb_version', PCFB_VERSION );
    }

    public static function deactivate() {
        // Cleanup if needed
    }

    private static function set_charset() {
        global $wpdb;
        self::$charset_collate = $wpdb->get_charset_collate();
    }

    private static function create_forms_table() {
        global $wpdb;

        $table_name = $wpdb->prefix . 'pcfb_forms';

        $sql = "CREATE TABLE {$table_name} (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            description text NOT NULL,
            settings longtext NOT NULL,
            status varchar(20) DEFAULT 'active' NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id)
        ) " . self::$charset_collate . ";";

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

    private static function create_fields_table() {
        global $wpdb;

        $table_name = $wpdb->prefix . 'pcfb_fields';

        $sql = "CREATE TABLE {$table_name} (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            form_id mediumint(9) NOT NULL,
            field_type varchar(50) NOT NULL,
            field_label varchar(255) NOT NULL,
            field_name varchar(255) NOT NULL,
            placeholder varchar(255) DEFAULT '',
            options longtext DEFAULT NULL,
            validation_rules longtext DEFAULT NULL,
            sort_order mediumint(9) DEFAULT 0 NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id),
            KEY form_id (form_id)
        ) " . self::$charset_collate . ";";

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

    private static function create_responses_table() {
        global $wpdb;

        $table_name = $wpdb->prefix . 'pcfb_responses';

        $sql = "CREATE TABLE {$table_name} (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            form_id mediumint(9) NOT NULL,
            user_ip varchar(100) DEFAULT '',
            user_agent text DEFAULT '',
            status varchar(20) DEFAULT 'new' NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id),
            KEY form_id (form_id)
        ) " . self::$charset_collate . ";";

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

    private static function create_response_data_table() {
        global $wpdb;

        $table_name = $wpdb->prefix . 'pcfb_response_data';

        $sql = "CREATE TABLE {$table_name} (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            response_id mediumint(9) NOT NULL,
            field_id mediumint(9) NOT NULL,
            field_value longtext NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id),
            KEY response_id (response_id),
            KEY field_id (field_id)
        ) " . self::$charset_collate . ";";

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

    public static function drop_tables() {
        global $wpdb;

        $tables = array(
            $wpdb->prefix . 'pcfb_response_data',
            $wpdb->prefix . 'pcfb_responses',
            $wpdb->prefix . 'pcfb_fields',
            $wpdb->prefix . 'pcfb_forms',
        );

        foreach ( $tables as $table ) {
            $wpdb->query( "DROP TABLE IF EXISTS {$table}" );
        }
    }
}
