-- Migration: Add country and currency fields
-- Run this to update existing database

USE patrimony_apitest;

-- Add country and currency to users table
ALTER TABLE users 
ADD COLUMN IF NOT EXISTS country VARCHAR(10) DEFAULT 'OM' AFTER phone_number,
ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'OMR' AFTER country,
ADD INDEX idx_users_country (country);

-- Add country and currency to car_listings table
ALTER TABLE car_listings 
ADD COLUMN IF NOT EXISTS country VARCHAR(10) NOT NULL DEFAULT 'OM' AFTER user_id,
ADD COLUMN IF NOT EXISTS currency VARCHAR(3) NOT NULL DEFAULT 'OMR' AFTER country,
ADD INDEX idx_listings_country (country),
ADD INDEX idx_listings_currency (currency);

-- Update existing records to have default values
UPDATE users SET country = 'OM', currency = 'OMR' WHERE country IS NULL OR currency IS NULL;
UPDATE car_listings SET country = 'OM', currency = 'OMR' WHERE country IS NULL OR currency IS NULL;

