<?php
/**
 * Class LSC_Activator
 * Handles plugin activation: database table creation, default options.
 */
class LSC_Activator {

    /**
     * Run on plugin activation.
     */
    public static function activate() {
        self::create_tables();
        self::set_defaults();
        LSC_Roles::add_roles();
        flush_rewrite_rules();
    }

    /**
     * Run on plugin deactivation.
     */
    public static function deactivate() {
        flush_rewrite_rules();
    }

    /**
     * Create custom database tables.
     */
    private static function create_tables() {
        global $wpdb;

        $charset_collate = $wpdb->get_charset_collate();

        // Chats table.
        $table_chats = $wpdb->prefix . 'lsc_chats';
        $sql_chats   = "CREATE TABLE {$table_chats} (
            id          BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            visitor_id  VARCHAR(64)         NOT NULL,
            visitor_name VARCHAR(100)        DEFAULT '',
            visitor_email VARCHAR(200)       DEFAULT '',
            status      VARCHAR(20)         NOT NULL DEFAULT 'open',
            assigned_to BIGINT(20) UNSIGNED DEFAULT NULL,
            created_at  DATETIME            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at  DATETIME            NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY visitor_id  (visitor_id),
            KEY status      (status),
            KEY assigned_to (assigned_to)
        ) {$charset_collate};";

        // Messages table.
        $table_messages = $wpdb->prefix . 'lsc_messages';
        $sql_messages   = "CREATE TABLE {$table_messages} (
            id         BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            chat_id    BIGINT(20) UNSIGNED NOT NULL,
            sender     VARCHAR(20)         NOT NULL DEFAULT 'visitor',
            content    TEXT                NOT NULL,
            is_read    TINYINT(1)          NOT NULL DEFAULT 0,
            created_at DATETIME            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY chat_id    (chat_id),
            KEY sender     (sender),
            KEY is_read    (is_read)
        ) {$charset_collate};";

        // Sessions table (chatter login log).
        $table_sessions = $wpdb->prefix . 'lsc_sessions';
        $sql_sessions   = "CREATE TABLE {$table_sessions} (
            id           BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id      BIGINT(20) UNSIGNED NOT NULL,
            user_login   VARCHAR(60)         NOT NULL DEFAULT '',
            display_name VARCHAR(250)        NOT NULL DEFAULT '',
            login_at     DATETIME            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            logout_at    DATETIME                     DEFAULT NULL,
            ip_address   VARCHAR(45)         NOT NULL DEFAULT '',
            PRIMARY KEY (id),
            KEY user_id  (user_id),
            KEY login_at (login_at)
        ) {$charset_collate};";

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta( $sql_chats );
        dbDelta( $sql_messages );
        dbDelta( $sql_sessions );

        update_option( 'lsc_db_version', LSC_VERSION );
    }

    /**
     * Set default plugin options.
     */
    private static function set_defaults() {
        if ( false === get_option( 'lsc_chat_status' ) ) {
            update_option( 'lsc_chat_status', 'offline' );
        }
        if ( false === get_option( 'lsc_widget_position' ) ) {
            update_option( 'lsc_widget_position', 'bottom-right' );
        }
        if ( false === get_option( 'lsc_widget_text' ) ) {
            update_option( 'lsc_widget_text', 'How Can We Help You' );
        }
        if ( false === get_option( 'lsc_offline_message' ) ) {
            update_option( 'lsc_offline_message', 'We are currently offline. Please leave a message.' );
        }
    }
}
