-- Migration script to add company model to existing schema
-- This script should be run after the Go schema changes have been applied to the database

-- 1. Ensure company dimension types exist (they should be inserted by init_defaults.go, but just in case)
INSERT IGNORE INTO master_company_dimension (dimension_id, dimension_type, dimension_code, dimension_name, dimension_desc, sort_order, is_active, created_at, updated_at, data_date)
VALUES
(1000, 'COMPANY_TYPE', 'SUPPLIER', '供应商', '供应商类型', 1, 1, NOW(), NULL, CURDATE()),
(1001, 'COMPANY_TYPE', 'SUBSIDIARY', '子公司', '子公司类型', 2, 1, NOW(), NULL, CURDATE()),
(1002, 'COMPANY_TYPE', 'AGENT', '代理商', '代理商类型', 3, 1, NOW(), NULL, CURDATE()),
(1003, 'COMPANY_TYPE', 'WHOLESALER', '批发商', '批发商类型', 4, 1, NOW(), NULL, CURDATE()),
(1004, 'COMPANY_TYPE', 'GROUP', '集团', '集团类型', 5, 1, NOW(), NULL, CURDATE()),
(1005, 'COMPANY_TYPE', 'FRANCHISEE', '加盟商', '加盟商类型', 6, 1, NOW(), NULL, CURDATE()),
(1006, 'COMPANY_TYPE', 'CUSTOMER', '客户', '客户类型', 7, 1, NOW(), NULL, CURDATE()),
(1007, 'COMPANY_TYPE', 'LOGISTICS', '物流公司', '物流公司类型', 8, 1, NOW(), NULL, CURDATE());

-- 2. Create a default company for existing stores (if no company exists)
INSERT IGNORE INTO master_company (company_id, company_code, company_name, company_type, tax_id, contact_name, contact_phone, address, is_active, created_at, updated_at, data_date)
SELECT
    CONCAT('COMP_DEFAULT_', s.shop_id) as company_id,
    CONCAT('DEFAULT_', s.shop_code) as company_code,
    CONCAT(s.shop_name, ' (默认公司)') as company_name,
    'SUBSIDIARY' as company_type,
    NULL as tax_id,
    s.manager_name as contact_name,
    s.contact_phone as contact_phone,
    s.address,
    1 as is_active,
    NOW() as created_at,
    NULL as updated_at,
    CURDATE() as data_date
FROM master_store s
WHERE NOT EXISTS (SELECT 1 FROM master_company c WHERE c.company_id = s.shop_id)
LIMIT 1; -- Only create one default company for all stores? Actually each store may belong to different companies. We'll assign a single default company for now.

-- 3. Update store table with company_id from default company
UPDATE master_store s
SET company_id = (SELECT company_id FROM master_company WHERE company_code LIKE 'DEFAULT_%' LIMIT 1)
WHERE company_id IS NULL OR company_id = '';

-- 4. Create companies for distinct suppliers from purchase receipts
INSERT IGNORE INTO master_company (company_id, company_code, company_name, company_type, tax_id, contact_name, contact_phone, address, is_active, created_at, updated_at, data_date)
SELECT DISTINCT
    supplier_id as company_id,
    supplier_code as company_code,
    supplier_name as company_name,
    'SUPPLIER' as company_type,
    NULL as tax_id,
    NULL as contact_name,
    NULL as contact_phone,
    NULL as address,
    1 as is_active,
    NOW() as created_at,
    NULL as updated_at,
    CURDATE() as data_date
FROM bill_purchase_receipt
WHERE supplier_id IS NOT NULL AND supplier_id != ''
  AND NOT EXISTS (SELECT 1 FROM master_company c WHERE c.company_id = bill_purchase_receipt.supplier_id);

-- 5. Update purchase receipt table with from_company_id (supplier) and to_company_id (store's company)
UPDATE bill_purchase_receipt bpr
JOIN master_store s ON bpr.shop_id = s.shop_id
SET bpr.from_company_id = bpr.supplier_id,
    bpr.to_company_id = s.company_id
WHERE bpr.from_company_id IS NULL OR bpr.from_company_id = '';

-- 6. Update wholesale sales table with from_company_id (store's company) and to_company_id (customer company)
-- Note: wholesale sales currently have supplier_id? Actually wholesale sales may have customer fields. We'll use shop's company as from_company, and create companies for customers if needed.
-- First create companies for distinct wholesale customers
INSERT IGNORE INTO master_company (company_id, company_code, company_name, company_type, tax_id, contact_name, contact_phone, address, is_active, created_at, updated_at, data_date)
SELECT DISTINCT
    customer_id as company_id,
    customer_code as company_code,
    customer_name as company_name,
    'CUSTOMER' as company_type,
    NULL as tax_id,
    NULL as contact_name,
    NULL as contact_phone,
    NULL as address,
    1 as is_active,
    NOW() as created_at,
    NULL as updated_at,
    CURDATE() as data_date
FROM bill_sales_wholesale
WHERE customer_id IS NOT NULL AND customer_id != ''
  AND NOT EXISTS (SELECT 1 FROM master_company c WHERE c.company_id = bill_sales_wholesale.customer_id);

-- Update wholesale sales with company IDs
UPDATE bill_sales_wholesale bsw
JOIN master_store s ON bsw.shop_id = s.shop_id
SET bsw.from_company_id = s.company_id,
    bsw.to_company_id = bsw.customer_id
WHERE bsw.from_company_id IS NULL OR bsw.from_company_id = '';

-- 7. Update stock transfer tables with company IDs
-- For transfers, from_company_id is the source store's company, to_company_id is destination store's company
UPDATE bill_stock_transfer_ship bsts
JOIN master_store s_src ON bsts.from_shop_id = s_src.shop_id
JOIN master_store s_dest ON bsts.to_shop_id = s_dest.shop_id
SET bsts.from_company_id = s_src.company_id,
    bsts.to_company_id = s_dest.company_id
WHERE bsts.from_company_id IS NULL OR bsts.from_company_id = '';

UPDATE bill_stock_transfer_receive bstr
JOIN master_store s_src ON bstr.from_shop_id = s_src.shop_id
JOIN master_store s_dest ON bstr.to_shop_id = s_dest.shop_id
SET bstr.from_company_id = s_src.company_id,
    bstr.to_company_id = s_dest.company_id
WHERE bstr.from_company_id IS NULL OR bstr.from_company_id = '';

-- 8. Update inventory count table with company_id from store
UPDATE bill_inventory_count bic
JOIN master_store s ON bic.shop_id = s.shop_id
SET bic.company_id = s.company_id
WHERE bic.company_id IS NULL OR bic.company_id = '';

-- 9. Update retail sales table with company_id from store
UPDATE bill_sales_retail bsr
JOIN master_store s ON bsr.shop_id = s.shop_id
SET bsr.company_id = s.company_id
WHERE bsr.company_id IS NULL OR bsr.company_id = '';

-- 10. Optional: Clean up old supplier fields from product table (if you want to remove them physically, but we already removed from schema)
-- ALTER TABLE master_product DROP COLUMN supplier_id, DROP COLUMN supplier_code, DROP COLUMN supplier_name;

-- Migration completed
SELECT 'Company schema migration completed successfully' as message;