BEGIN;

CREATE OR REPLACE FUNCTION _tmp_wo(p_name text, p_seq int, p_match text, p_acct int, p_tax int, p_label text) RETURNS void AS $$
DECLARE v_mid int; v_lid int;
BEGIN
  INSERT INTO account_reconcile_model (name, sequence, company_id, trigger, match_label, match_label_param, active, can_be_proposed, create_uid, write_uid, create_date, write_date)
  VALUES (jsonb_build_object('en_US', p_name), p_seq, 1, 'auto_reconcile', 'contains', p_match, true, true, 2, 2, NOW(), NOW()) RETURNING id INTO v_mid;
  INSERT INTO account_reconcile_model_line (model_id, company_id, sequence, account_id, amount_type, amount, amount_string, label, create_uid, write_uid, create_date, write_date)
  VALUES (v_mid, 1, 10, p_acct, 'percentage', 100, '100', jsonb_build_object('en_US', p_label), 2, 2, NOW(), NOW()) RETURNING id INTO v_lid;
  INSERT INTO account_reconcile_model_line_account_tax_rel (account_reconcile_model_line_id, account_tax_id) VALUES (v_lid, p_tax);
END; $$ LANGUAGE plpgsql;

-- ============================================================
-- FIX 1: Wawanesa (model 28) — convert from partner_map to writeoff
-- Insurance → Car Insurance (548), NO TAX (insurance is exempt)
-- ============================================================
-- Deactivate old partner_map model
UPDATE account_reconcile_model SET active = false WHERE id = 28;

-- Create new writeoff model for Wawanesa
SELECT _tmp_wo('Wawanesa Insurance Premium', 800, 'WAWANESA', 548, 32, 'Wawanesa Car Insurance Premium');

-- ============================================================
-- FIX 2: Personal Loan SPL (model 80) — fix match param
-- "Personal Loan SPL" doesn't match "Personal Loan : SPL"
-- ============================================================
UPDATE account_reconcile_model SET match_label_param = 'Personal Loan' WHERE id = 80;

-- ============================================================
-- FIX 3: IFS Insurance (model 23) — same issue, convert from partner_map
-- ============================================================
-- Check if model 23 has writeoff line
-- Model 23: match "IFS PREMIUM", mapped_partner_id=7291, no writeoff line
UPDATE account_reconcile_model SET active = false WHERE id = 23;
SELECT _tmp_wo('IFS Insurance Premium', 801, 'IFS PREMIUM', 550, 32, 'IFS Commercial Insurance Premium');

-- ============================================================
-- NEW MODELS for remaining repeated patterns
-- ============================================================

-- Telus Bill Payment (523 = Telephone, HST)
SELECT _tmp_wo('Telus Bill Payment', 802, 'Telus Comm', 523, 20, 'Telus Communications Bill Payment');

-- e-Transfer fee (already model 5 but check if matching)
-- Model 5 matches "e-Transfer fee" — should work

-- Account Payable Pmt HOME (LTC home customer payments → Outstanding Receipts)
SELECT _tmp_wo('HOME LTC Customer Payment', 803, 'Account Payable PmtHOME', 493, 32, 'HOME Long-Term Care Payment');
SELECT _tmp_wo('HOME LTC Customer Payment 2', 804, 'Account Payable Pmt HOME', 493, 32, 'HOME Long-Term Care Payment');
SELECT _tmp_wo('HOME LTC Customer Payment 3', 805, 'Account Payable Pmt-HOME', 493, 32, 'HOME Long-Term Care Payment');

-- R & M Health Supplies payment
SELECT _tmp_wo('R&M Health Supplies Payment', 806, 'R & M HEALTH', 493, 32, 'R&M Health Supplies Customer Payment');

-- BCCL payment
SELECT _tmp_wo('BCCL Customer Payment', 807, 'Account Payable Pmt-BCCL', 493, 32, 'BCCL Customer Payment');

-- CARE payment
SELECT _tmp_wo('CARE Customer Payment', 808, 'Account Payable PmtCARE', 493, 32, 'CARE Customer Payment');

-- Amica Senior Life (different spelling from earlier model)
SELECT _tmp_wo('Amica Senior Life Payment', 809, 'AMICASENIORLIFE', 493, 32, 'Amica Senior Life Customer Payment');

-- Cash withdrawal (no tax, Office Expense)
SELECT _tmp_wo('Cash Withdrawal', 810, 'Cash withdrawal', 507, 32, 'Cash Withdrawal');

-- ATM/Mobile adjustment
SELECT _tmp_wo('ATM Mobile Adjustment Credit', 811, 'ATM/Mobile adjustment credit', 499, 32, 'ATM Mobile Adjustment Credit');
SELECT _tmp_wo('ATM Mobile Adjustment Debit', 812, 'ATM/Mobile adjustment debit', 499, 32, 'ATM Mobile Adjustment Debit');

-- e-Transfer cancel (returned funds → Outstanding Receipts)
SELECT _tmp_wo('e-Transfer Cancellation', 813, 'e-Transfer cancel', 493, 32, 'e-Transfer Cancelled Return');

-- OnRoute (highway rest stop meals)
SELECT _tmp_wo('OnRoute Highway Meals', 814, 'ONROUTE', 506, 20, 'OnRoute Highway Rest Stop');

-- Opening Balance
SELECT _tmp_wo('Opening Balance', 815, 'Opening Balance', 493, 32, 'Opening Balance Entry');

-- Foreign Exchange withdrawal
SELECT _tmp_wo('Royal Foreign Exchange', 816, 'Royal Foreign Exchange', 525, 32, 'Royal Foreign Exchange Withdrawal');

-- Online Banking wire payment
SELECT _tmp_wo('Online Banking Wire Payment', 817, 'Online Banking wire', 494, 32, 'Online Banking Wire Payment');

-- Henrys camera store refund
SELECT _tmp_wo('Henrys Camera Refund', 818, 'Henry', 507, 20, 'Henrys Camera Store');

DROP FUNCTION _tmp_wo(text, int, text, int, int, text);

COMMIT;
