-- Existing installation patch for partial payments + WhatsApp toggle/test settings support
-- Safe to import from phpMyAdmin on an already-installed database.
-- It only adds missing columns/settings and backfills payment amounts/status.

SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS;
SET FOREIGN_KEY_CHECKS = 0;

DROP PROCEDURE IF EXISTS apply_existing_install_patch;
DELIMITER $$
CREATE PROCEDURE apply_existing_install_patch()
BEGIN
    -- users.avatar
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'users'
          AND COLUMN_NAME = 'avatar'
    ) THEN
        ALTER TABLE users
            ADD COLUMN avatar VARCHAR(255) DEFAULT NULL;
    END IF;

    -- branches.avatar
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'branches'
          AND COLUMN_NAME = 'avatar'
    ) THEN
        ALTER TABLE branches
            ADD COLUMN avatar VARCHAR(255) DEFAULT NULL;
    END IF;

    -- sales_orders.customer_id
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'sales_orders'
          AND COLUMN_NAME = 'customer_id'
    ) THEN
        ALTER TABLE sales_orders
            ADD COLUMN customer_id INT(10) UNSIGNED DEFAULT NULL;
    END IF;

    -- sales_orders.payment_status
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'sales_orders'
          AND COLUMN_NAME = 'payment_status'
    ) THEN
        ALTER TABLE sales_orders
            ADD COLUMN payment_status VARCHAR(20) NOT NULL DEFAULT 'paid';
    END IF;

    -- sales_orders.vat_amount
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'sales_orders'
          AND COLUMN_NAME = 'vat_amount'
    ) THEN
        ALTER TABLE sales_orders
            ADD COLUMN vat_amount DECIMAL(10,3) NOT NULL DEFAULT 0.000 AFTER subtotal;
    END IF;

    -- sales_orders.paid_amount
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'sales_orders'
          AND COLUMN_NAME = 'paid_amount'
    ) THEN
        ALTER TABLE sales_orders
            ADD COLUMN paid_amount DECIMAL(10,3) NOT NULL DEFAULT 0.000 AFTER total_amount;
    END IF;

    -- sales_orders.due_amount
    IF NOT EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = 'sales_orders'
          AND COLUMN_NAME = 'due_amount'
    ) THEN
        ALTER TABLE sales_orders
            ADD COLUMN due_amount DECIMAL(10,3) NOT NULL DEFAULT 0.000 AFTER paid_amount;
    END IF;

    -- Backfill / normalize payment values for existing invoices.
    UPDATE sales_orders
    SET paid_amount = CASE
        WHEN payment_status = 'unpaid' THEN 0
        ELSE total_amount
    END
    WHERE paid_amount IS NULL OR paid_amount = 0;

    UPDATE sales_orders
    SET due_amount = GREATEST(total_amount - paid_amount, 0);

    UPDATE sales_orders
    SET payment_status = CASE
        WHEN due_amount <= 0.0005 THEN 'paid'
        WHEN paid_amount > 0 THEN 'partial'
        ELSE 'unpaid'
    END;

    -- Settings keys used by the new WhatsApp tab and toggle.
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_enabled', '1');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_token', '');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_secret_key', '');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_template_created', '');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_template_pending', '');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_template_accepted', '');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_template_completed', '');
    INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('wablas_template_rejected', '');
END $$
DELIMITER ;

CALL apply_existing_install_patch();
DROP PROCEDURE IF EXISTS apply_existing_install_patch;

SET FOREIGN_KEY_CHECKS = @OLD_FOREIGN_KEY_CHECKS;

-- Optional verification after import:
-- SHOW COLUMNS FROM sales_orders;
-- SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'wablas%';
