-- Migration 043: Add States Table
--
-- Creates the states table if it does not exist.
-- Safe to run multiple times (idempotent).
--
-- Run with:
--   CANNAIQ_DB_URL="postgresql://..." psql $CANNAIQ_DB_URL -f migrations/043_add_states_table.sql

-- =====================================================
-- 1) CREATE STATES TABLE
-- =====================================================
CREATE TABLE IF NOT EXISTS states (
    id SERIAL PRIMARY KEY,
    code TEXT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- =====================================================
-- 2) INSERT CORE US STATES
-- =====================================================
INSERT INTO states (code, name) VALUES
    ('AZ', 'Arizona'),
    ('CA', 'California'),
    ('CO', 'Colorado'),
    ('FL', 'Florida'),
    ('IL', 'Illinois'),
    ('MA', 'Massachusetts'),
    ('MD', 'Maryland'),
    ('MI', 'Michigan'),
    ('MO', 'Missouri'),
    ('NV', 'Nevada'),
    ('NJ', 'New Jersey'),
    ('NY', 'New York'),
    ('OH', 'Ohio'),
    ('OK', 'Oklahoma'),
    ('OR', 'Oregon'),
    ('PA', 'Pennsylvania'),
    ('WA', 'Washington')
ON CONFLICT (code) DO NOTHING;

-- =====================================================
-- 3) ADD INDEX
-- =====================================================
CREATE INDEX IF NOT EXISTS idx_states_code ON states(code);

-- =====================================================
-- DONE
-- =====================================================
