-- Import SIE4 data for LandveX AB FY2026
-- This script imports the existing bookkeeping data

-- Create tenant for LandveX AB
INSERT INTO boc_tenants (name, slug, domain, settings)
VALUES ('LandveX AB', 'landvex', 'landvex.com', '{"org_number": "559141-7042", "currency": "SEK"}')
ON CONFLICT (slug) DO UPDATE SET name = 'LandveX AB';

-- Get tenant ID
DO $$
DECLARE
    v_tenant_id UUID;
    v_fiscal_year_id UUID;
BEGIN
    SELECT id INTO v_tenant_id FROM boc_tenants WHERE slug = 'landvex';
    
    -- Create fiscal year 2026
    INSERT INTO boc_fiscal_years (tenant_id, year, start_date, end_date)
    VALUES (v_tenant_id, 2026, '2026-01-01', '2026-12-31')
    ON CONFLICT (tenant_id, year) DO NOTHING;
    
    SELECT id INTO v_fiscal_year_id FROM boc_fiscal_years WHERE tenant_id = v_tenant_id AND year = 2026;
    
    -- Insert BAS accounts from SIE4
    INSERT INTO boc_accounts (tenant_id, account_number, name, account_type, vat_code) VALUES
    (v_tenant_id, '1221', 'Datorer och kringutrustning', 'asset', NULL),
    (v_tenant_id, '1227', 'Personbilar', 'asset', NULL),
    (v_tenant_id, '1930', 'Företagskonto / affärskonto', 'asset', NULL),
    (v_tenant_id, '2510', 'Skatteskulder', 'liability', NULL),
    (v_tenant_id, '2611', 'Utgående moms 25%', 'liability', '25'),
    (v_tenant_id, '2990', 'Övriga skulder till närstående / ägaruttag', 'liability', NULL),
    (v_tenant_id, '3000', 'Försäljning av tjänster', 'income', '25'),
    (v_tenant_id, '3010', 'Konsultarvode', 'income', '25'),
    (v_tenant_id, '3020', 'Hyresintäkter', 'income', '25'),
    (v_tenant_id, '3900', 'Övriga rörelseintäkter', 'income', '25'),
    (v_tenant_id, '5420', 'Personalrepresentation', 'expense', NULL),
    (v_tenant_id, '5460', 'Arbetskläder och skyddsmaterial', 'expense', NULL),
    (v_tenant_id, '5612', 'Fordonsskatt', 'expense', NULL),
    (v_tenant_id, '5614', 'Bilförsäkring', 'expense', NULL),
    (v_tenant_id, '5810', 'Resekostnader', 'expense', NULL),
    (v_tenant_id, '5820', 'Biljettkostnader', 'expense', NULL),
    (v_tenant_id, '5900', 'Reklam och marknadsföring', 'expense', NULL),
    (v_tenant_id, '6071', 'Representation avdragsgill', 'expense', NULL),
    (v_tenant_id, '6540', 'IT-tjänster, köpta', 'expense', '25'),
    (v_tenant_id, '6550', 'Programvarulicenser', 'expense', '25'),
    (v_tenant_id, '7630', 'Friskvård', 'expense', NULL),
    (v_tenant_id, '8910', 'Skatt på årets resultat', 'expense', NULL)
    ON CONFLICT (tenant_id, account_number) DO NOTHING;
    
    RAISE NOTICE 'LandveX AB tenant and accounts created';
END $$;