<?php
// If this file is called directly, abort.

use Anbarak\Tables;

if (!defined('WPINC')) {
    die;
}

global $anbarak_db_version;
$anbarak_db_version = ANBARAK_VERSION;

function anbarak_install()
{
    global $wpdb;
    global $anbarak_db_version;

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    $charset_collate = $wpdb->get_charset_collate();

    // Create orders table
    $table_name = $wpdb->prefix . Tables::ORDERS;
    $sql = "CREATE TABLE $table_name (
        id BIGINT(20) NOT NULL AUTO_INCREMENT,
        orderid INT(10) NOT NULL,
        time INT(11) NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";
    dbDelta($sql);

    // Create expenses table
    $table_name = $wpdb->prefix . Tables::EXPENSES;
    $sql = "CREATE TABLE $table_name (
        id MEDIUMINT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
        product_id BIGINT(20),
        price DECIMAL(18, 2) NOT NULL,
        qty INT(11),
        party_id BIGINT UNSIGNED,
        notes TEXT,
        tag_id INT(11),
        consumed_qty INT DEFAULT 0,
        invoice_number VARCHAR(255) DEFAULT NULL,
        buy_discount DECIMAL(10, 2) DEFAULT NULL,
        tax_rate DECIMAL(10, 2) DEFAULT NULL,
        invoice_date DATE DEFAULT NULL,
        account_date DATE DEFAULT NULL,
        payment_method VARCHAR(50) DEFAULT NULL, 
        payment_reference VARCHAR(255) DEFAULT NULL,
        exchange_rate DECIMAL(10, 2) DEFAULT NULL,
        timestamp INT(11) NOT NULL,
        deleted_at INT(11),
        PRIMARY KEY (id),
        KEY idx_product_id (product_id),
        KEY idx_available_qty (product_id, qty, consumed_qty, deleted_at),
        KEY idx_tag_id (tag_id)
    ) $charset_collate;";
    dbDelta($sql);

    // Create expenses tag table
    $table_name = $wpdb->prefix . Tables::EXPENSES_TAGS;
    $sql = "CREATE TABLE $table_name (
        id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
        name VARCHAR(200) COLLATE utf8mb4_unicode_ci,
        color VARCHAR(255),
        deleted_at INT(11) DEFAULT NULL,
        PRIMARY KEY (id),
        UNIQUE KEY name (name)
    ) $charset_collate;";
    dbDelta($sql);

    $tags_table = $wpdb->prefix . Tables::EXPENSES_TAGS;
    // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom table name is fixed from $wpdb->prefix and Tables; WordPress 6.0 does not support %i placeholders.
    $tags_column = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM {$tags_table} LIKE %s", 'deleted_at'));
    if (empty($tags_column)) {
        // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema update against a fixed custom table name.
        $wpdb->query("ALTER TABLE {$tags_table} ADD deleted_at INT(11) DEFAULT NULL AFTER color");
    }

    add_option('anbarak_version', $anbarak_db_version);

    // create default purchase tag
    anbarak_rebuild_product_expenses_tags();
}

function anbarak_activate()
{
    // update Tables 
    anbarak_install();

    update_option('anbarak_version', ANBARAK_VERSION);
}

register_activation_hook('anbarak/anbarak.php', 'anbarak_activate');
