-- Migration 064: Expand Google API data columns (GBP Performance + GSC extensions + Owner Data)
-- Part of Google OAuth 100% coverage initiative

-- === GBP PERFORMANCE API ===
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_impressions_maps INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_impressions_search INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_call_clicks INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_website_clicks INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_direction_requests INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_conversations INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_search_keywords JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gbp_performance_period_days INTEGER DEFAULT 30;

-- === GOOGLE SEARCH CONSOLE EXTENSIONS ===
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_top_pages JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_device_breakdown JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_index_status VARCHAR(50);
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_last_crawl TIMESTAMP;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_crawled_as VARCHAR(50);
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_sitemaps JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_country_breakdown JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_search_type_breakdown JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS gsc_trend_data JSONB;

-- === GBP OWNER DATA (Management API) ===
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS google_owner_responses_count INTEGER;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS google_review_response_rate NUMERIC(5,1);
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS google_posts_data JSONB;
ALTER TABLE company_website_analysis ADD COLUMN IF NOT EXISTS google_posts_count INTEGER;

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