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

class Manfiet_Expense_Tracker {
    public function __construct() {
        new Manfiet_Expense_API();
        new Manfiet_Expense_Admin_Menu();
    }

    // Create Custom Tables on Activation
    public static function install() {
        global $wpdb;
        $table_expenses = $wpdb->prefix . 'manfiet_expenses';
        $table_categories = $wpdb->prefix . 'manfiet_expense_categories';
        $table_tags = $wpdb->prefix . 'manfiet_expense_tags';
        $charset_collate = $wpdb->get_charset_collate();
    
        // Expense Table
        $sql1 = "CREATE TABLE IF NOT EXISTS $table_expenses (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            category VARCHAR(100) NOT NULL,
            amount DECIMAL(10,2) NOT NULL,
            date DATE NOT NULL,
            tags LONGTEXT NULL,
            recurring_interval VARCHAR(20) DEFAULT NULL,
            remark TEXT NULL,
            KEY user_id (user_id),
            KEY date (date)
        ) $charset_collate;";
    
        // Categories Table
        $sql2 = "CREATE TABLE IF NOT EXISTS $table_categories (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            category_name VARCHAR(100) NOT NULL,
            parent_id INT NOT NULL DEFAULT 0,
            description TEXT NULL,
            KEY user_id (user_id),
            KEY parent_id (parent_id)
        ) $charset_collate;";

        // Expense Tags Table
        $sql3 = "CREATE TABLE IF NOT EXISTS $table_tags (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            tag_name VARCHAR(100) NOT NULL,
            KEY user_id (user_id)
        ) $charset_collate;";
    
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta($sql1);
        dbDelta($sql2);
        dbDelta($sql3);
        // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Install-time schema checks/updates operate on plugin-owned tables derived from trusted $wpdb prefix.

        $tags_column_exists = $wpdb->get_var($wpdb->prepare("SHOW COLUMNS FROM {$table_expenses} LIKE %s", 'tags'));
        if (!$tags_column_exists) {
            $wpdb->query("ALTER TABLE {$table_expenses} ADD COLUMN tags LONGTEXT NULL");
        }

        $remark_column_exists = $wpdb->get_var($wpdb->prepare("SHOW COLUMNS FROM {$table_expenses} LIKE %s", 'remark'));
        if (!$remark_column_exists) {
            $wpdb->query("ALTER TABLE {$table_expenses} ADD COLUMN remark TEXT NULL");
        }

        $parent_column_exists = $wpdb->get_var($wpdb->prepare("SHOW COLUMNS FROM {$table_categories} LIKE %s", 'parent_id'));
        if (!$parent_column_exists) {
            $wpdb->query("ALTER TABLE {$table_categories} ADD COLUMN parent_id INT NOT NULL DEFAULT 0");
        }

        $description_column_exists = $wpdb->get_var($wpdb->prepare("SHOW COLUMNS FROM {$table_categories} LIKE %s", 'description'));
        if (!$description_column_exists) {
            $wpdb->query("ALTER TABLE {$table_categories} ADD COLUMN description TEXT NULL");
        }

        $parent_index_exists = $wpdb->get_var($wpdb->prepare("SHOW INDEX FROM {$table_categories} WHERE Key_name = %s", 'parent_id'));
        if (!$parent_index_exists) {
            $wpdb->query("ALTER TABLE {$table_categories} ADD KEY parent_id (parent_id)");
        }
        // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter
    
        if (!wp_next_scheduled('manfiet_process_recurring_expenses')) {
            wp_schedule_event(time(), 'daily', 'manfiet_process_recurring_expenses');
        }
    
        if (!wp_next_scheduled('manfiet_check_budget_alert')) {
            wp_schedule_event(time(), 'weekly', 'manfiet_check_budget_alert');
        }
    }
 
}
