-- Migration: Create regions and cities tables
-- Description: Global location data (Regions/States + Cities)

CREATE TABLE IF NOT EXISTS regions (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    country_code VARCHAR(10) NOT NULL,  -- ISO Country (US, BR, JP, etc.)
    code VARCHAR(10),  -- ISO Region code (e.g., SP, CA)
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS cities (
    id SERIAL PRIMARY KEY,
    region_id INT NOT NULL,
    name VARCHAR(100) NOT NULL,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    FOREIGN KEY (region_id) REFERENCES regions(id) ON DELETE CASCADE
);

-- Indexes
CREATE INDEX idx_regions_country ON regions(country_code);
CREATE INDEX idx_cities_region ON cities(region_id);

-- Comments
COMMENT ON TABLE regions IS 'Global Regions (States, Provinces, Prefectures)';
COMMENT ON TABLE cities IS 'Global Cities by Region';
COMMENT ON COLUMN regions.code IS 'ISO Region code (e.g., SP, CA)';
