<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class PexEmail_Activator {

    public static function activate() {
        global $wpdb;
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

        $charset_collate = $wpdb->get_charset_collate();

        // 1. Lists Table (For organizing contacts)
        $table_lists = $wpdb->prefix . 'pexemail_lists';
        $sql_lists = "CREATE TABLE $table_lists (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id)
        ) $charset_collate;";

        // 2. Contacts Table (First Name, Last Name, Email)
        $table_contacts = $wpdb->prefix . 'pexemail_contacts';
        $sql_contacts = "CREATE TABLE $table_contacts (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            list_id bigint(20) NOT NULL,
            email varchar(100) NOT NULL,
            first_name varchar(100) DEFAULT '',
            last_name varchar(100) DEFAULT '',
            status varchar(20) DEFAULT 'subscribed',
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY list_id (list_id)
        ) $charset_collate;";

        // 3. Campaigns Table (Added missing columns for scheduling and tracking)
        $table_campaigns = $wpdb->prefix . 'pexemail_campaigns';
        $sql_campaigns = "CREATE TABLE $table_campaigns (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            subject varchar(255) NOT NULL,
            template longtext NOT NULL,
            list_id bigint(20) DEFAULT 0,
            status varchar(20) DEFAULT 'draft',
            scheduled_at int(11) DEFAULT 0,
            opens int(11) DEFAULT 0,
            clicks int(11) DEFAULT 0,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id)
        ) $charset_collate;";

        // 4. Recipients / Tracking Table
        $table_recipients = $wpdb->prefix . 'pexemail_recipients';
        $sql_recipients = "CREATE TABLE $table_recipients (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            campaign_id bigint(20) NOT NULL,
            contact_id bigint(20) NOT NULL,
            email varchar(100) NOT NULL,
            status varchar(20) DEFAULT 'pending',
            opened tinyint(1) DEFAULT 0,
            clicked tinyint(1) DEFAULT 0,
            sent_at datetime DEFAULT NULL,
            PRIMARY KEY  (id),
            KEY campaign_id (campaign_id)
        ) $charset_collate;";

        // Execute DB Schema Creation
        dbDelta( $sql_lists );
        dbDelta( $sql_contacts );
        dbDelta( $sql_campaigns );
        dbDelta( $sql_recipients );

        // Setup Default Settings (Strictly blank for compliance)
        $default_settings = [
            'sender_name'  => '',
            'sender_email' => '',
            'header_image' => '',
            'smtp_enabled' => 'no'
        ];
        add_option( 'pexemail_settings', $default_settings );
        add_option( 'pexemail_license_status', 'inactive' );
    }
}