-- Add measurement fields and helpful indexes for model filters
CREATE EXTENSION IF NOT EXISTS pgcrypto;

DO $$
BEGIN
  IF to_regclass('public.models') IS NULL THEN
    RAISE NOTICE 'Table models does not exist yet. Run init.sql or 002 migration first.';
  ELSE
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='models' AND column_name='breast_size') THEN
      EXECUTE 'ALTER TABLE models ADD COLUMN breast_size TEXT';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='models' AND column_name='butt_size') THEN
      EXECUTE 'ALTER TABLE models ADD COLUMN butt_size TEXT';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='models' AND column_name='waist_size') THEN
      EXECUTE 'ALTER TABLE models ADD COLUMN waist_size TEXT';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='models' AND column_name='hip_size') THEN
      EXECUTE 'ALTER TABLE models ADD COLUMN hip_size TEXT';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='models' AND column_name='shoe_size') THEN
      EXECUTE 'ALTER TABLE models ADD COLUMN shoe_size TEXT';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='models' AND column_name='eye_color') THEN
      EXECUTE 'ALTER TABLE models ADD COLUMN eye_color TEXT';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename='models' AND indexname='idx_models_location') THEN
      EXECUTE 'CREATE INDEX idx_models_location ON models ((COALESCE(location, '''')))';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename='models' AND indexname='idx_models_featured') THEN
      EXECUTE 'CREATE INDEX idx_models_featured ON models (featured)';
    END IF;
  END IF;
END$$;
