

-- ============================================================
-- TABELA: events
-- ============================================================
CREATE TABLE IF NOT EXISTS events (
  id              SERIAL PRIMARY KEY,
  title           TEXT NOT NULL,
  description     TEXT NOT NULL,
  category        TEXT NOT NULL,
  venue           TEXT NOT NULL,
  city            TEXT NOT NULL,
  state           TEXT NOT NULL,
  date            TEXT NOT NULL,
  time            TEXT NOT NULL,
  image_url       TEXT,
  banner_image_url TEXT,
  kit_image_url   TEXT,
  latitude        DECIMAL(10,7),
  longitude       DECIMAL(10,7),
  status          TEXT NOT NULL DEFAULT 'published',
  total_capacity  INTEGER NOT NULL,
  organizer_name  TEXT NOT NULL,
  organizer_id    INTEGER REFERENCES users(id),
  kit_pickup_info TEXT,
  regulation      TEXT,
  rejection_reason TEXT,
  created_at      TIMESTAMP NOT NULL DEFAULT NOW()
);

-- ============================================================
-- TABELA: ticket_types
-- ============================================================
CREATE TABLE IF NOT EXISTS ticket_types (
  id            SERIAL PRIMARY KEY,
  event_id      INTEGER NOT NULL REFERENCES events(id) ON DELETE CASCADE,
  name          TEXT NOT NULL,
  description   TEXT,
  price         DECIMAL(10,2) NOT NULL,
  quantity      INTEGER NOT NULL,
  sold          INTEGER NOT NULL DEFAULT 0,
  is_half_price BOOLEAN NOT NULL DEFAULT FALSE,
  sort_order    INTEGER NOT NULL DEFAULT 0
);

-- ============================================================
-- TABELA: orders
-- ============================================================
CREATE TABLE IF NOT EXISTS orders (
  id                SERIAL PRIMARY KEY,
  event_id          INTEGER NOT NULL REFERENCES events(id),
  buyer_name        TEXT NOT NULL,
  buyer_email       TEXT NOT NULL,
  buyer_cpf         TEXT NOT NULL,
  status            TEXT NOT NULL DEFAULT 'confirmed',
  total_amount      DECIMAL(10,2) NOT NULL,
  custom_fields_data JSONB,
  qr_code           TEXT,
  checked_in        BOOLEAN NOT NULL DEFAULT FALSE,
  checked_in_at     TIMESTAMP,
  kit_delivered     BOOLEAN NOT NULL DEFAULT FALSE,
  kit_delivered_at  TIMESTAMP,
  notes             TEXT,
  deleted_at        TIMESTAMP,
  created_at        TIMESTAMP NOT NULL DEFAULT NOW()
);

-- ============================================================
-- TABELA: order_items
-- ============================================================
CREATE TABLE IF NOT EXISTS order_items (
  id             SERIAL PRIMARY KEY,
  order_id       INTEGER NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
  ticket_type_id INTEGER NOT NULL REFERENCES ticket_types(id),
  quantity       INTEGER NOT NULL,
  unit_price     DECIMAL(10,2) NOT NULL,
  subtotal       DECIMAL(10,2) NOT NULL
);

-- ============================================================
-- TABELA: event_custom_fields
-- ============================================================
CREATE TABLE IF NOT EXISTS event_custom_fields (
  id         SERIAL PRIMARY KEY,
  event_id   INTEGER NOT NULL REFERENCES events(id) ON DELETE CASCADE,
  label      TEXT NOT NULL,
  field_type TEXT NOT NULL,
  required   BOOLEAN NOT NULL DEFAULT FALSE,
  options    JSONB,
  sort_order INTEGER NOT NULL DEFAULT 0
);

-- ============================================================
-- TABELA: organizer_documents
-- ============================================================
CREATE TABLE IF NOT EXISTS organizer_documents (
  id            SERIAL PRIMARY KEY,
  organizer_id  INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  document_type TEXT NOT NULL,
  file_name     TEXT NOT NULL,
  original_name TEXT NOT NULL,
  file_size     INTEGER,
  object_path   TEXT NOT NULL,
  uploaded_at   TIMESTAMP NOT NULL DEFAULT NOW()
);

-- ============================================================
-- TABELA: event_documents
-- ============================================================
CREATE TABLE IF NOT EXISTS event_documents (
  id            SERIAL PRIMARY KEY,
  event_id      INTEGER NOT NULL REFERENCES events(id) ON DELETE CASCADE,
  file_name     TEXT NOT NULL,
  original_name TEXT NOT NULL,
  file_size     INTEGER,
  object_path   TEXT NOT NULL,
  uploaded_at   TIMESTAMP NOT NULL DEFAULT NOW()
);

-- ============================================================
-- FIM DO SCRIPT
-- Login padrão: maria@teste.com / 123456
-- TROQUE A SENHA DO ADMIN APÓS O PRIMEIRO LOGIN!
-- ============================================================
