-- Retail CRM intelligence layer
CREATE TABLE IF NOT EXISTS supermarket_contacts (
    id SERIAL PRIMARY KEY,
    supermarket_id INTEGER NOT NULL REFERENCES supermarkets(id) ON DELETE CASCADE,
    role VARCHAR(64) NOT NULL DEFAULT 'manager',
    full_name VARCHAR(255),
    phone VARCHAR(32),
    email VARCHAR(255),
    linkedin_url VARCHAR(512),
    source VARCHAR(100),
    confidence NUMERIC(3,2) DEFAULT 0.50,
    verified_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_supermarket_contacts_store ON supermarket_contacts(supermarket_id);
CREATE INDEX IF NOT EXISTS idx_supermarket_contacts_role ON supermarket_contacts(role);

CREATE TABLE IF NOT EXISTS supermarket_profiles (
    supermarket_id INTEGER PRIMARY KEY REFERENCES supermarkets(id) ON DELETE CASCADE,
    staff_count_estimate INTEGER,
    manager_name VARCHAR(255),
    manager_phone VARCHAR(32),
    manager_email VARCHAR(255),
    services JSONB DEFAULT '{}'::jsonb,
    facilities JSONB DEFAULT '{}'::jsonb,
    web_data JSONB DEFAULT '{}'::jsonb,
    scrape_url VARCHAR(512),
    last_scraped_at TIMESTAMPTZ,
    data_completeness NUMERIC(3,2) DEFAULT 0,
    notes TEXT
);

CREATE TABLE IF NOT EXISTS client_supermarket_links (
    id SERIAL PRIMARY KEY,
    client_id INTEGER NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
    supermarket_id INTEGER NOT NULL REFERENCES supermarkets(id) ON DELETE CASCADE,
    deal_id INTEGER REFERENCES deals(id) ON DELETE SET NULL,
    relationship_type VARCHAR(64) DEFAULT 'prospect',
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE(client_id, supermarket_id)
);
CREATE INDEX IF NOT EXISTS idx_csl_client ON client_supermarket_links(client_id);
CREATE INDEX IF NOT EXISTS idx_csl_store ON client_supermarket_links(supermarket_id);

CREATE TABLE IF NOT EXISTS halal_certifications (
    id SERIAL PRIMARY KEY,
    supermarket_id INTEGER REFERENCES supermarkets(id) ON DELETE SET NULL,
    certifier VARCHAR(100) NOT NULL,
    certificate_number VARCHAR(100),
    business_name VARCHAR(255),
    address TEXT,
    city VARCHAR(100),
    expiry_date DATE,
    status VARCHAR(32) DEFAULT 'active',
    registry_source VARCHAR(100),
    matched_confidence NUMERIC(3,2),
    raw_data JSONB DEFAULT '{}'::jsonb,
    created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_halal_cert_store ON halal_certifications(supermarket_id);
CREATE INDEX IF NOT EXISTS idx_halal_cert_status ON halal_certifications(status);

CREATE TABLE IF NOT EXISTS retail_opportunity_scores (
    supermarket_id INTEGER PRIMARY KEY REFERENCES supermarkets(id) ON DELETE CASCADE,
    halal_opportunity_score NUMERIC(5,2),
    market_potential_score NUMERIC(5,2),
    factors JSONB DEFAULT '{}'::jsonb,
    computed_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_retail_opp_halal ON retail_opportunity_scores(halal_opportunity_score DESC);

ALTER TABLE supermarkets ADD COLUMN IF NOT EXISTS employee_count INTEGER;
ALTER TABLE supermarkets ADD COLUMN IF NOT EXISTS manager_name VARCHAR(255);
ALTER TABLE supermarkets ADD COLUMN IF NOT EXISTS enrichment_score NUMERIC(3,2) DEFAULT 0;
ALTER TABLE supermarkets ADD COLUMN IF NOT EXISTS halal_opportunity_score NUMERIC(5,2);

INSERT INTO data_providers (name, provider_type, config, is_active) VALUES
    ('halal_registry', 'scrape', '{"sources": ["osm", "hqc", "manual"]}', true),
    ('branch_scraper', 'scrape', '{"chains": ["ah", "jumbo", "plus", "lidl", "aldi", "dirk"]}', true),
    ('market_trends', 'api', '{"categories": ["kant-en-klaar", "halal", "ready-meals"]}', true)
ON CONFLICT (name) DO UPDATE SET config = EXCLUDED.config, is_active = true;
