-- GeoSeek database draft for PostgreSQL.
-- This file is intentionally schema-only; do not run on production before adapting migrations.

CREATE TABLE IF NOT EXISTS geo_categories (
  id BIGSERIAL PRIMARY KEY,
  kategori TEXT NOT NULL UNIQUE,
  subkategori_default TEXT,
  target_total INTEGER DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS geo_regions (
  id BIGSERIAL PRIMARY KEY,
  provinsi TEXT NOT NULL,
  kota TEXT NOT NULL,
  kecamatan TEXT,
  kelurahan TEXT,
  latitude NUMERIC(10, 7),
  longitude NUMERIC(10, 7),
  sumber_data TEXT DEFAULT 'GeoSeek',
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS geo_places (
  id BIGSERIAL PRIMARY KEY,
  nama_tempat TEXT NOT NULL,
  kategori TEXT NOT NULL,
  subkategori TEXT,
  alamat TEXT,
  kecamatan TEXT,
  kota TEXT,
  provinsi TEXT,
  latitude NUMERIC(10, 7),
  longitude NUMERIC(10, 7),
  no_telp TEXT,
  jam_operasional TEXT,
  rating NUMERIC(2, 1),
  jumlah_review INTEGER DEFAULT 0,
  sumber_data TEXT NOT NULL DEFAULT 'GeoSeek',
  status_verifikasi TEXT NOT NULL DEFAULT 'unverified',
  status TEXT NOT NULL DEFAULT 'aktif',
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_geo_places_keyword
  ON geo_places USING GIN (to_tsvector('simple', coalesce(nama_tempat, '') || ' ' || coalesce(kategori, '') || ' ' || coalesce(subkategori, '') || ' ' || coalesce(alamat, '')));

CREATE INDEX IF NOT EXISTS idx_geo_places_location
  ON geo_places (latitude, longitude);

CREATE INDEX IF NOT EXISTS idx_geo_places_region
  ON geo_places (provinsi, kota, kecamatan);

CREATE TABLE IF NOT EXISTS geo_reviews (
  id BIGSERIAL PRIMARY KEY,
  place_id BIGINT NOT NULL REFERENCES geo_places(id) ON DELETE CASCADE,
  user_id UUID,
  rating NUMERIC(2, 1),
  comment TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);
