<?php
/**
 * Fired during plugin activation.
 * This class defines all code necessary to run during the plugin's activation.
 * It replaces the old install-script.php file.
 *
 * @package    Event_Monster
 * @subpackage Event_Monster/includes
 */
class Event_Monster_Free_Activator
{

    /**
     * The main activation method.
     * This method creates all necessary database tables and sets default options.
     */
    public static function activate()
    {
        global $wpdb;
        // dbDelta is the WordPress function for creating/updating tables.
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        $charset_collate = $wpdb->get_charset_collate();

        // 1. Create Tickets Table
        $em_ticket_table = $wpdb->prefix . 'em_tickets';
        $sql_tickets = "CREATE TABLE $em_ticket_table (
            id int(11) NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            `desc` text NOT NULL,
            price float NOT NULL,
            status varchar(20) NOT NULL,
            extra text NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta($sql_tickets);

        // 2. Create Coupons Table (used by Pro version, kept for compatibility)
        $em_coupon_table = $wpdb->prefix . 'em_coupons';
        $sql_coupons = "CREATE TABLE $em_coupon_table (
            id int(11) NOT NULL AUTO_INCREMENT,
            coupon_code varchar(255) NOT NULL,
            discount_value int(11) NOT NULL,
            discount_type varchar(20) NOT NULL,
            Comment varchar(255) NOT NULL,
            start_date date NOT NULL,
            expiry_date date NOT NULL,
            total_coupons int(11) NOT NULL,
            used_coupons int(11) NOT NULL,
            status varchar(20) NOT NULL,
            extra text NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta($sql_coupons);

        // 3. Create Bookings Table
        $em_booking_table = $wpdb->prefix . 'em_bookings';
        $sql_bookings = "CREATE TABLE $em_booking_table (
            id int(11) NOT NULL AUTO_INCREMENT,
            event_id int(11) NOT NULL,
            ticket_id text NOT NULL,
            amount decimal(10,2) NOT NULL,
            coupon_id int(11) NOT NULL,
            attendees_id int(11) NOT NULL,
            booking_type varchar(255) NOT NULL,
            payment_id int(11) NOT NULL,
            status varchar(255) NOT NULL,
            date_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
            extra text NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta($sql_bookings);

        // 4. Create Payments Table (used by Pro version, kept for compatibility)
        $em_payment_table = $wpdb->prefix . 'em_payments';
        $sql_payments = "CREATE TABLE $em_payment_table (
            id int(11) NOT NULL AUTO_INCREMENT,
            txn_id varchar(255) NOT NULL,
            event_id int(11) NOT NULL,
            attendee_id int(11) NOT NULL,
            ticket_id text NOT NULL,
            payee_name varchar(255) NOT NULL,
            payee_email varchar(255) NOT NULL,
            amount float NOT NULL,
            paid float NOT NULL,
            discount float NOT NULL,
            payment_gatway varchar(255) NOT NULL,
            status varchar(255) NOT NULL,
            extra text NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta($sql_payments);

        // 5. Create Attendees Table
        $em_attendee_table = $wpdb->prefix . 'em_attendees';
        $sql_attendees = "CREATE TABLE $em_attendee_table (
            id int(11) NOT NULL AUTO_INCREMENT,
            event_id int(11) NOT NULL,
            ticket_id text NOT NULL,
            first_name varchar(255) NOT NULL,
            last_name varchar(255) NOT NULL,
            email varchar(255) NOT NULL,
            phone varchar(255) NOT NULL,
            extra text NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta($sql_attendees);

        // 6. Create Currency Table
        $em_currency_table = $wpdb->prefix . "em_currency";
        $sql_currency = "CREATE TABLE $em_currency_table (
            id int(11) NOT NULL AUTO_INCREMENT,
            currency_name varchar(30) NOT NULL,
            code varchar(3) NOT NULL,
            symbol varchar(20) NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta($sql_currency);

        // 7. Insert Default Data (only if the table is empty)
        if ($wpdb->get_var("SELECT COUNT(*) FROM $em_currency_table") == 0) {
            $wpdb->query("INSERT INTO `$em_currency_table` (`id`, `currency_name`, `code`, `symbol`) VALUES (1, 'United States dollar', 'USD', '&#36;'), (2, 'Euro', 'EUR', '&euro;'), (3, 'Japanese yen', 'JPY', '&yen;'), (4, 'British pound', 'GBP', '&#163;'), (5, 'Indian rupee', 'INR', '&#8377;')");
            // Add other currencies as needed
        }

        if ($wpdb->get_var("SELECT COUNT(*) FROM $em_ticket_table") == 0) {
            $wpdb->insert($em_ticket_table, ['name' => 'Free Ticket', 'desc' => 'This is a sample free ticket.', 'price' => 0, 'status' => 'available']);
            $wpdb->insert($em_ticket_table, ['name' => 'Paid Ticket', 'desc' => 'This is a sample paid ticket.', 'price' => 5, 'status' => 'available']);
        }

        // 8. Set up default options
        if (!get_option('awl_em_common_settings')) {
            $emdefaultsettings = array(
                "em_form_heading_one" => "Book Ticket Now",
                "em_form_sucsess_msg_field" => "Your Booking has been completed!!",
                "email_carrier" => "php", // Starter version uses PHP mail only
                "em_notification_client" => "on",
                "em_notification_admin" => "on",
                "em_payment_mode" => "free_registration", // Starter version is free registration only
                "em_currency_code" => "USD",
            );
            add_option('awl_em_common_settings', $emdefaultsettings);
        }

        // 9. Set the database version for fresh installs
        if (!get_option('em_starter_db_version')) {
            add_option('em_starter_db_version', EM_FREE_VERSION);
        }

        // 10. Flush rewrite rules
        flush_rewrite_rules();
    }
}