import { pool } from './src/db/migrate'; async function addGeoFields() { console.log('πŸ—ΊοΈ Adding geo-location fields...\n'); try { await pool.query(` ALTER TABLE stores ADD COLUMN IF NOT EXISTS latitude DECIMAL(10, 8), ADD COLUMN IF NOT EXISTS longitude DECIMAL(11, 8), ADD COLUMN IF NOT EXISTS region VARCHAR(100), ADD COLUMN IF NOT EXISTS market_area VARCHAR(255), ADD COLUMN IF NOT EXISTS timezone VARCHAR(50) `); console.log('βœ… Added geo fields to stores table'); // Create indexes for geo queries await pool.query(` CREATE INDEX IF NOT EXISTS idx_stores_location ON stores(latitude, longitude) WHERE latitude IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_stores_city_state ON stores(city, state); CREATE INDEX IF NOT EXISTS idx_stores_region ON stores(region); CREATE INDEX IF NOT EXISTS idx_stores_market ON stores(market_area); `); console.log('βœ… Created geo indexes'); // Create location-based views await pool.query(` CREATE OR REPLACE VIEW stores_by_region AS SELECT region, state, COUNT(*) as store_count, COUNT(DISTINCT city) as cities, array_agg(DISTINCT name ORDER BY name) as store_names FROM stores WHERE active = true GROUP BY region, state ORDER BY store_count DESC `); await pool.query(` CREATE OR REPLACE VIEW market_coverage AS SELECT city, state, zip, COUNT(*) as dispensaries, array_agg(name ORDER BY name) as store_names, COUNT(DISTINCT id) as unique_stores FROM stores WHERE active = true GROUP BY city, state, zip ORDER BY dispensaries DESC `); console.log('βœ… Created location views'); console.log('\nβœ… Geo-location setup complete!'); console.log('\nπŸ“Š Available location views:'); console.log(' - stores_by_region: Stores grouped by region/state'); console.log(' - market_coverage: Dispensary density by city'); console.log('\nπŸ’‘ Your database now supports:'); console.log(' βœ… Lead Generation (contact info + locations)'); console.log(' βœ… Market Research (pricing + inventory data)'); console.log(' βœ… Investment Planning (market coverage + trends)'); console.log(' βœ… Retail Partner Discovery (store directory)'); console.log(' βœ… Geo-targeted Campaigns (lat/long + regions)'); console.log(' βœ… Trend Analysis (price history + timestamps)'); console.log(' βœ… Directory/App Creation (full store catalog)'); console.log(' βœ… Delivery Optimization (locations + addresses)'); } catch (error) { console.error('❌ Error:', error); } finally { await pool.end(); } } addGeoFields();