-- Migration 036: Add fields for findadispo.com frontend
-- These fields are needed for the consumer-facing dispensary locator UI
-- This migration is idempotent - safe to run multiple times

-- Add hours as JSONB to support structured hours data
-- Example: {"monday": {"open": "09:00", "close": "21:00"}, "tuesday": {...}, ...}
-- Or simple: {"formatted": "Mon-Sat 9am-9pm, Sun 10am-6pm"}
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'hours') THEN
        ALTER TABLE dispensaries ADD COLUMN hours JSONB DEFAULT NULL;
    END IF;
END $$;

-- Add amenities as TEXT array
-- Example: ['Wheelchair Accessible', 'ATM', 'Online Ordering', 'Curbside Pickup', 'Delivery']
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'amenities') THEN
        ALTER TABLE dispensaries ADD COLUMN amenities TEXT[] DEFAULT '{}';
    END IF;
END $$;

-- Add description for the "About" section on dispensary detail pages
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'description') THEN
        ALTER TABLE dispensaries ADD COLUMN description TEXT DEFAULT NULL;
    END IF;
END $$;

-- Add image_url for the main dispensary hero image
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'dispensaries' AND column_name = 'image_url') THEN
        ALTER TABLE dispensaries ADD COLUMN image_url TEXT DEFAULT NULL;
    END IF;
END $$;

-- Add indexes for filtering (IF NOT EXISTS is supported for indexes in PG 9.5+)
CREATE INDEX IF NOT EXISTS idx_dispensaries_amenities ON dispensaries USING GIN (amenities);

-- Add comments for documentation (COMMENT is idempotent - re-running just updates the comment)
COMMENT ON COLUMN dispensaries.hours IS 'Store hours in JSONB format. Can be structured by day or formatted string.';
COMMENT ON COLUMN dispensaries.amenities IS 'Array of amenity tags like Wheelchair Accessible, ATM, Online Ordering, etc.';
COMMENT ON COLUMN dispensaries.description IS 'Description text for dispensary detail page About section.';
COMMENT ON COLUMN dispensaries.image_url IS 'URL to main dispensary image for hero/card display.';
