-- ============================================================
-- Migration: 012_pkd_integration.sql
-- Date: 2026-01-13
-- Description: Add PKD codes and CEIDG owner data to companies
-- ============================================================

-- PKD (Polska Klasyfikacja Działalności) - main business activity code
ALTER TABLE companies ADD COLUMN IF NOT EXISTS pkd_code VARCHAR(10);
ALTER TABLE companies ADD COLUMN IF NOT EXISTS pkd_description TEXT;

-- Business start date from CEIDG
ALTER TABLE companies ADD COLUMN IF NOT EXISTS business_start_date DATE;

-- JDG owner info from CEIDG (only for sole proprietorships without KRS)
ALTER TABLE companies ADD COLUMN IF NOT EXISTS owner_first_name VARCHAR(100);
ALTER TABLE companies ADD COLUMN IF NOT EXISTS owner_last_name VARCHAR(100);

-- Add comments for documentation
COMMENT ON COLUMN companies.pkd_code IS 'Primary PKD code from CEIDG (e.g., 6201Z, 4321Z)';
COMMENT ON COLUMN companies.pkd_description IS 'Polish description of main business activity from CEIDG';
COMMENT ON COLUMN companies.business_start_date IS 'Business start date from CEIDG (data_rozpoczecia)';
COMMENT ON COLUMN companies.owner_first_name IS 'JDG owner first name from CEIDG (for sole proprietorships)';
COMMENT ON COLUMN companies.owner_last_name IS 'JDG owner last name from CEIDG (for sole proprietorships)';

-- Create index for PKD code lookup (useful for filtering by industry)
CREATE INDEX IF NOT EXISTS idx_companies_pkd_code ON companies(pkd_code);

-- Grant permissions
GRANT ALL ON TABLE companies TO nordabiz_app;

-- ============================================================
-- Verification query (run after migration):
-- SELECT name, pkd_code, pkd_description, business_start_date, owner_first_name, owner_last_name
-- FROM companies WHERE pkd_code IS NOT NULL LIMIT 5;
-- ============================================================
