-- ============================================================================
-- TaxCore Configuration Setup
-- ============================================================================
-- This migration creates the TaxCore configuration needed for invoice submission
-- Replace the placeholder values with your actual TaxCore credentials

DO $$
DECLARE
    v_org_id uuid;
BEGIN
    -- Get the first org (you may need to adjust this)
    SELECT org_id INTO v_org_id FROM orgs LIMIT 1;
    
    IF v_org_id IS NULL THEN
        RAISE EXCEPTION 'No organization found. Please create an organization first.';
    END IF;

    -- Insert TaxCore configuration
    INSERT INTO taxcore_configurations (
        org_id,
        certificate_path,
        certificate_password,
        api_base_url,
        api_timeout_seconds,
        tin,
        business_name,
        location_name,
        mrc,
        pos_number,
        is_active,
        auto_submit,
        submit_delay_seconds,
        certificate_expires_at
    ) VALUES (
        v_org_id,
        '/path/to/your/certificate.p12',           -- TODO: Update with actual certificate path
        'YOUR_CERTIFICATE_PASSWORD',               -- TODO: Update with actual password
        'https://vsdc-uat.frcs.org.fj',           -- UAT environment (change to prod when ready)
        30,                                        -- API timeout in seconds
        'YOUR_TIN_NUMBER',                         -- TODO: Update with your TIN
        'YOUR_BUSINESS_NAME',                      -- TODO: Update with your business name
        'Main Office',                             -- Location name
        'YOUR_MRC_NUMBER',                         -- TODO: Update with your MRC
        'POS-01',                                  -- POS number
        false,                                     -- Set to false initially - enable after testing
        false,                                     -- Auto-submit disabled initially
        0,                                         -- No delay
        (CURRENT_DATE + INTERVAL '1 year')::date  -- Certificate expiry (adjust as needed)
    )
    ON CONFLICT (org_id) DO UPDATE SET
        updated_at = now();

    RAISE NOTICE 'TaxCore configuration created for org_id: %', v_org_id;
    RAISE NOTICE '';
    RAISE NOTICE '⚠️  IMPORTANT: Update the following placeholders in taxcore_configurations:';
    RAISE NOTICE '   1. certificate_path - Path to your .p12 certificate file';
    RAISE NOTICE '   2. certificate_password - Your certificate password';
    RAISE NOTICE '   3. tin - Your Tax Identification Number';
    RAISE NOTICE '   4. business_name - Your registered business name';
    RAISE NOTICE '   5. mrc - Your Merchant Registration Code';
    RAISE NOTICE '';
    RAISE NOTICE 'After updating, set is_active = true to enable TaxCore integration.';
    RAISE NOTICE '';
    RAISE NOTICE 'To update: UPDATE taxcore_configurations SET certificate_path = ''...'', ... WHERE org_id = ''%'';', v_org_id;
    
END $$;
