-- Add columns to track which row from Google Sheets has been synced
-- Run this SQL if the columns don't exist yet

-- Check if columns exist, if not add them
ALTER TABLE leads 
ADD COLUMN IF NOT EXISTS sheet_row_id INT DEFAULT NULL COMMENT 'Row number from Google Sheets',
ADD COLUMN IF NOT EXISTS source_sheet VARCHAR(50) DEFAULT NULL COMMENT 'Sheet name (Sheet1 or snap form data)';

-- Create index for faster lookups
CREATE INDEX IF NOT EXISTS idx_sheet_row ON leads(source_sheet, sheet_row_id);

-- Optional: View to see sync status
CREATE OR REPLACE VIEW v_sync_status AS
SELECT 
    source_sheet,
    COUNT(*) as total_rows,
    MAX(sheet_row_id) as last_synced_row,
    MAX(created_at) as last_sync_time
FROM leads
WHERE source_sheet IS NOT NULL
GROUP BY source_sheet;

