-- Migration 040: Add dba_name column to dispensaries table
-- DBA (Doing Business As) name - the name the dispensary operates under,
-- which may differ from the legal entity name
-- This migration is idempotent - safe to run multiple times

-- Add dba_name column
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'dba_name') THEN
        ALTER TABLE dispensaries ADD COLUMN dba_name TEXT DEFAULT NULL;
    END IF;
END $$;

-- Add company_name column (legal entity name)
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'company_name') THEN
        ALTER TABLE dispensaries ADD COLUMN company_name TEXT DEFAULT NULL;
    END IF;
END $$;

-- Add azdhs_id for Arizona Department of Health Services license number
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'azdhs_id') THEN
        ALTER TABLE dispensaries ADD COLUMN azdhs_id INTEGER DEFAULT NULL;
    END IF;
END $$;

-- Add phone column
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'phone') THEN
        ALTER TABLE dispensaries ADD COLUMN phone TEXT DEFAULT NULL;
    END IF;
END $$;

-- Add email column
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'email') THEN
        ALTER TABLE dispensaries ADD COLUMN email TEXT DEFAULT NULL;
    END IF;
END $$;

-- Add google_rating column
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'google_rating') THEN
        ALTER TABLE dispensaries ADD COLUMN google_rating NUMERIC(2,1) DEFAULT NULL;
    END IF;
END $$;

-- Add google_review_count column
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'google_review_count') THEN
        ALTER TABLE dispensaries ADD COLUMN google_review_count INTEGER DEFAULT NULL;
    END IF;
END $$;

-- Add comments for documentation
COMMENT ON COLUMN dispensaries.dba_name IS 'DBA (Doing Business As) name - the public-facing name the dispensary operates under';
COMMENT ON COLUMN dispensaries.company_name IS 'Legal entity/company name that owns the dispensary';
COMMENT ON COLUMN dispensaries.azdhs_id IS 'Arizona Department of Health Services license number';
COMMENT ON COLUMN dispensaries.phone IS 'Contact phone number';
COMMENT ON COLUMN dispensaries.email IS 'Contact email address';
COMMENT ON COLUMN dispensaries.google_rating IS 'Google Maps rating (1.0 to 5.0)';
COMMENT ON COLUMN dispensaries.google_review_count IS 'Number of Google reviews';

-- Create index for searching by dba_name
CREATE INDEX IF NOT EXISTS idx_dispensaries_dba_name ON dispensaries (dba_name);
CREATE INDEX IF NOT EXISTS idx_dispensaries_azdhs_id ON dispensaries (azdhs_id);
