-- ============================================================================
-- ERP GENERIC - SEED DATA: SAMPLE DATA (Development)
-- ============================================================================
-- Description: Sample partners, products, and transactions for testing
-- ============================================================================

-- ===========================================
-- UUID REFERENCE (from previous seeds)
-- ===========================================
-- TENANT_DEMO: 1c7dfbb0-19b8-4e87-a225-a74da6f26dbf
-- COMPANY_DEMO: 50fa9b29-504f-4c45-8f8a-3d129cfc6095

-- ===========================================
-- SAMPLE PARTNERS (Customers & Vendors)
-- ===========================================

-- Customer 1 - Acme Corporation
INSERT INTO core.partners (id, tenant_id, name, legal_name, partner_type, is_customer, is_supplier, is_company, email, phone, tax_id, created_at)
VALUES (
    'dda3e76c-0f92-49ea-b647-62fde7d6e1d1',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'Acme Corporation',
    'Acme Corporation S.A. de C.V.',
    'company',
    true,
    false,
    true,
    'ventas@acme.mx',
    '+52 55 1111 2222',
    'ACM123456ABC',
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

-- Customer 2 - Tech Solutions
INSERT INTO core.partners (id, tenant_id, name, legal_name, partner_type, is_customer, is_supplier, is_company, email, phone, tax_id, created_at)
VALUES (
    '78291258-da01-4560-a49e-5047d92cf11f',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'Tech Solutions',
    'Tech Solutions de México S.A.',
    'company',
    true,
    false,
    true,
    'contacto@techsolutions.mx',
    '+52 55 3333 4444',
    'TSM987654XYZ',
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

-- Vendor 1 - Materiales del Centro
INSERT INTO core.partners (id, tenant_id, name, legal_name, partner_type, is_customer, is_supplier, is_company, email, phone, tax_id, created_at)
VALUES (
    '643c97e3-bf44-40ed-bd01-ae1f5f0d861b',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'Materiales del Centro',
    'Materiales del Centro S. de R.L.',
    'company',
    false,
    true,
    true,
    'ventas@materialescentro.mx',
    '+52 55 5555 6666',
    'MDC456789DEF',
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

-- Vendor 2 - Distribuidora Nacional
INSERT INTO core.partners (id, tenant_id, name, legal_name, partner_type, is_customer, is_supplier, is_company, email, phone, tax_id, created_at)
VALUES (
    '79f3d083-375e-4e50-920b-a3630f74d4b1',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    'Distribuidora Nacional',
    'Distribuidora Nacional de Productos S.A.',
    'company',
    false,
    true,
    true,
    'pedidos@distnacional.mx',
    '+52 55 7777 8888',
    'DNP321654GHI',
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

-- ===========================================
-- SAMPLE PRODUCT CATEGORIES
-- ===========================================

INSERT INTO core.product_categories (id, tenant_id, name, code, parent_id, full_path, active, created_at)
VALUES
    ('f10ee8c4-e52e-41f5-93b3-a140d09dd807', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', 'All Products', 'ALL', NULL, 'All Products', true, CURRENT_TIMESTAMP),
    ('b1517141-470a-4835-98ff-9250ffd18121', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', 'Raw Materials', 'RAW', 'f10ee8c4-e52e-41f5-93b3-a140d09dd807', 'All Products / Raw Materials', true, CURRENT_TIMESTAMP),
    ('0b55e26b-ec64-4a80-aab3-be5a55b0ca88', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', 'Finished Goods', 'FIN', 'f10ee8c4-e52e-41f5-93b3-a140d09dd807', 'All Products / Finished Goods', true, CURRENT_TIMESTAMP),
    ('e92fbdc8-998f-4bf2-8a00-c7efd3e8eb64', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', 'Services', 'SRV', 'f10ee8c4-e52e-41f5-93b3-a140d09dd807', 'All Products / Services', true, CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;

-- ===========================================
-- SAMPLE PRODUCTS
-- ===========================================

INSERT INTO inventory.products (id, tenant_id, name, code, barcode, category_id, product_type, uom_id, cost_price, list_price, created_at)
VALUES
    -- Product 1: Raw material - Steel Sheet
    (
        'ccbc64d7-06f9-47a1-9ad7-6dbfbbf82955',
        '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
        'Steel Sheet 4x8',
        'MAT-001',
        '7501234567890',
        'b1517141-470a-4835-98ff-9250ffd18121',
        'storable',
        (SELECT id FROM core.uom WHERE code = 'unit' LIMIT 1),
        350.00,
        500.00,
        CURRENT_TIMESTAMP
    ),
    -- Product 2: Finished good - Metal Cabinet
    (
        '1d4bbccb-1d83-4b15-a85d-687e378fff96',
        '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
        'Metal Cabinet Large',
        'PROD-001',
        '7501234567891',
        '0b55e26b-ec64-4a80-aab3-be5a55b0ca88',
        'storable',
        (SELECT id FROM core.uom WHERE code = 'unit' LIMIT 1),
        1800.00,
        2500.00,
        CURRENT_TIMESTAMP
    ),
    -- Product 3: Service - Installation
    (
        'aae17b73-5bd2-433e-bb99-d9187df398b8',
        '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
        'Installation Service',
        'SRV-001',
        NULL,
        'e92fbdc8-998f-4bf2-8a00-c7efd3e8eb64',
        'service',
        (SELECT id FROM core.uom WHERE code = 'h' LIMIT 1),
        300.00,
        500.00,
        CURRENT_TIMESTAMP
    )
ON CONFLICT (id) DO NOTHING;

-- ===========================================
-- SAMPLE WAREHOUSE & LOCATIONS
-- ===========================================

INSERT INTO inventory.warehouses (id, tenant_id, company_id, name, code, is_default, active, created_at)
VALUES (
    '40ea2e44-31aa-4e4c-856d-e5c3dd0b942f',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    '50fa9b29-504f-4c45-8f8a-3d129cfc6095',
    'Main Warehouse',
    'WH-MAIN',
    true,
    true,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO inventory.locations (id, tenant_id, warehouse_id, name, complete_name, location_type, active, created_at)
VALUES
    ('7a57d418-4ea6-47d7-a3e0-2ade4c95e240', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', '40ea2e44-31aa-4e4c-856d-e5c3dd0b942f', 'Stock', 'WH-MAIN/Stock', 'internal', true, CURRENT_TIMESTAMP),
    ('3bea067b-5023-474b-88cf-97bb0461538b', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', '40ea2e44-31aa-4e4c-856d-e5c3dd0b942f', 'Input', 'WH-MAIN/Input', 'internal', true, CURRENT_TIMESTAMP),
    ('8f97bcf7-a34f-406e-8292-bfb04502a4f8', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', '40ea2e44-31aa-4e4c-856d-e5c3dd0b942f', 'Output', 'WH-MAIN/Output', 'internal', true, CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;

-- ===========================================
-- SAMPLE STOCK QUANTITIES
-- ===========================================

DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'inventory' AND table_name = 'stock_quants') THEN
        -- Steel Sheet 4x8 - 100 units in Stock location
        PERFORM inventory.update_stock_quant(
            'ccbc64d7-06f9-47a1-9ad7-6dbfbbf82955'::uuid,
            '7a57d418-4ea6-47d7-a3e0-2ade4c95e240'::uuid,
            NULL,
            100.00
        );
        -- Metal Cabinet Large - 25 units in Stock location
        PERFORM inventory.update_stock_quant(
            '1d4bbccb-1d83-4b15-a85d-687e378fff96'::uuid,
            '7a57d418-4ea6-47d7-a3e0-2ade4c95e240'::uuid,
            NULL,
            25.00
        );
        RAISE NOTICE 'Stock quantities added via update_stock_quant function';
    ELSE
        RAISE NOTICE 'inventory.stock_quants table does not exist, skipping stock initialization';
    END IF;
END $$;

-- ===========================================
-- SAMPLE ANALYTIC ACCOUNTS
-- ===========================================

INSERT INTO analytics.analytic_plans (id, tenant_id, company_id, name, description, active, created_at)
VALUES (
    'f6085dcd-dcd7-4ef5-affc-0fa3b037b1d2',
    '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf',
    '50fa9b29-504f-4c45-8f8a-3d129cfc6095',
    'Projects',
    'Plan for project-based analytics',
    true,
    CURRENT_TIMESTAMP
) ON CONFLICT (id) DO NOTHING;

INSERT INTO analytics.analytic_accounts (id, tenant_id, company_id, plan_id, name, code, account_type, status, created_at)
VALUES
    ('858e16c0-773d-4cec-ac94-0241ab0c90e3', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', 'f6085dcd-dcd7-4ef5-affc-0fa3b037b1d2', 'Project Alpha', 'PROJ-ALPHA', 'project', 'active', CURRENT_TIMESTAMP),
    ('41b6a320-021d-473d-b643-038b1bb86055', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', 'f6085dcd-dcd7-4ef5-affc-0fa3b037b1d2', 'Project Beta', 'PROJ-BETA', 'project', 'active', CURRENT_TIMESTAMP),
    ('b950ada5-2f11-4dd7-a91b-5696dbb8fabc', '1c7dfbb0-19b8-4e87-a225-a74da6f26dbf', '50fa9b29-504f-4c45-8f8a-3d129cfc6095', 'f6085dcd-dcd7-4ef5-affc-0fa3b037b1d2', 'Operations', 'OPS', 'department', 'active', CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;

-- Output confirmation
DO $$
BEGIN
    RAISE NOTICE 'Sample data loaded:';
    RAISE NOTICE '  - 4 partners (2 customers, 2 vendors)';
    RAISE NOTICE '  - 4 product categories';
    RAISE NOTICE '  - 3 products';
    RAISE NOTICE '  - 1 warehouse with 3 locations';
    RAISE NOTICE '  - 3 analytic accounts';
END $$;
