# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2026_05_31_140000) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "pgcrypto"
  enable_extension "plpgsql"

  create_table "active_storage_attachments", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.uuid "record_id", null: false
    t.uuid "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.string "service_name", null: false
    t.bigint "byte_size", null: false
    t.string "checksum"
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "active_storage_variant_records", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "blob_id", null: false
    t.string "variation_digest", null: false
    t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
  end

  create_table "admin_accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "username", null: false
    t.string "password_digest", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["username"], name: "index_admin_accounts_on_username", unique: true
  end

  create_table "billing_invoices", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "club_id", null: false
    t.uuid "billing_payment_id"
    t.string "number", null: false
    t.date "issued_on", null: false
    t.integer "amount_cents", null: false
    t.string "currency", default: "eur", null: false
    t.string "status", default: "draft", null: false
    t.string "source", default: "manual", null: false
    t.string "aruba_document_id"
    t.text "notes"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.datetime "emailed_at"
    t.index ["billing_payment_id"], name: "index_billing_invoices_on_billing_payment_id"
    t.index ["billing_payment_id"], name: "index_billing_invoices_unique_payment", unique: true, where: "(billing_payment_id IS NOT NULL)"
    t.index ["club_id", "issued_on"], name: "index_billing_invoices_on_club_id_and_issued_on"
    t.index ["club_id", "number"], name: "index_billing_invoices_on_club_id_and_number", unique: true
    t.index ["club_id"], name: "index_billing_invoices_on_club_id"
  end

  create_table "billing_payments", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "club_id", null: false
    t.string "stripe_invoice_id"
    t.string "stripe_payment_intent_id"
    t.string "stripe_charge_id"
    t.integer "amount_cents", null: false
    t.string "currency", default: "eur", null: false
    t.string "status", default: "paid", null: false
    t.string "plan_slug"
    t.string "description"
    t.datetime "paid_at"
    t.string "invoice_pdf_url"
    t.string "receipt_url"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["club_id", "paid_at"], name: "index_billing_payments_on_club_id_and_paid_at"
    t.index ["club_id"], name: "index_billing_payments_on_club_id"
    t.index ["stripe_invoice_id"], name: "index_billing_payments_on_stripe_invoice_id", unique: true, where: "(stripe_invoice_id IS NOT NULL)"
  end

  create_table "club_memberships", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "user_id", null: false
    t.uuid "club_id", null: false
    t.string "role", default: "owner", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["club_id"], name: "index_club_memberships_on_club_id"
    t.index ["user_id", "club_id"], name: "index_club_memberships_on_user_id_and_club_id", unique: true
    t.index ["user_id"], name: "index_club_memberships_on_user_id"
  end

  create_table "clubs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name", null: false
    t.string "sport", default: "volleyball", null: false
    t.string "logo_url"
    t.string "primary_color", default: "#e53935", null: false
    t.string "secondary_color", default: "#ffffff", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "billing_entity_type", default: "company", null: false
    t.string "billing_legal_name"
    t.string "billing_vat_number"
    t.string "billing_fiscal_code"
    t.string "billing_email"
    t.string "billing_phone"
    t.string "billing_address_line"
    t.string "billing_city"
    t.string "billing_province", limit: 2
    t.string "billing_postal_code"
    t.string "billing_country", default: "IT", null: false
    t.string "billing_recipient_code", limit: 7
    t.string "billing_pec"
  end

  create_table "device_states", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "stream_session_id", null: false
    t.string "device_role", null: false
    t.integer "battery_level"
    t.string "network_type"
    t.integer "signal_strength"
    t.integer "current_bitrate"
    t.integer "target_bitrate"
    t.integer "fps"
    t.datetime "last_seen_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["stream_session_id", "device_role"], name: "index_device_states_on_stream_session_id_and_device_role", unique: true
    t.index ["stream_session_id"], name: "index_device_states_on_stream_session_id"
  end

  create_table "matches", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "team_id", null: false
    t.string "opponent_name", null: false
    t.string "location"
    t.datetime "scheduled_at"
    t.string "sport", default: "volleyball"
    t.integer "sets_to_win", default: 3
    t.jsonb "roster_numbers", default: []
    t.string "category"
    t.string "phase"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.jsonb "scoring_rules", default: {}, null: false
    t.index ["team_id"], name: "index_matches_on_team_id"
  end

  create_table "plans", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "slug", null: false
    t.string "name", null: false
    t.string "stripe_price_id"
    t.jsonb "features", default: {}, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["slug"], name: "index_plans_on_slug", unique: true
  end

  create_table "recordings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "stream_session_id", null: false
    t.uuid "team_id", null: false
    t.string "status", default: "processing", null: false
    t.string "storage_path"
    t.datetime "expires_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["expires_at"], name: "index_recordings_on_expires_at"
    t.index ["stream_session_id"], name: "index_recordings_on_stream_session_id", unique: true
    t.index ["team_id"], name: "index_recordings_on_team_id"
  end

  create_table "score_states", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "stream_session_id", null: false
    t.integer "home_sets", default: 0
    t.integer "away_sets", default: 0
    t.integer "home_points", default: 0
    t.integer "away_points", default: 0
    t.integer "current_set", default: 1
    t.string "period"
    t.boolean "timeout_home", default: false
    t.boolean "timeout_away", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.jsonb "set_partials", default: [], null: false
    t.index ["stream_session_id"], name: "index_score_states_on_stream_session_id", unique: true
  end

  create_table "stream_events", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "stream_session_id", null: false
    t.string "event_type", null: false
    t.jsonb "metadata", default: {}
    t.datetime "occurred_at", null: false
    t.index ["stream_session_id", "occurred_at"], name: "index_stream_events_on_stream_session_id_and_occurred_at"
    t.index ["stream_session_id"], name: "index_stream_events_on_stream_session_id"
  end

  create_table "stream_sessions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "match_id", null: false
    t.uuid "user_id", null: false
    t.string "status", default: "idle", null: false
    t.string "platform", default: "youtube"
    t.text "stream_key_encrypted"
    t.string "rtmp_url"
    t.string "youtube_broadcast_id"
    t.string "youtube_stream_id"
    t.string "privacy_status", default: "unlisted"
    t.string "quality_preset", default: "720p_30_2.5mbps"
    t.integer "target_bitrate", default: 2500000
    t.integer "target_fps", default: 30
    t.datetime "started_at"
    t.datetime "ended_at"
    t.integer "total_duration_secs", default: 0
    t.integer "disconnection_count", default: 0
    t.string "publish_token"
    t.string "pairing_token_digest"
    t.datetime "pairing_token_expires_at"
    t.string "timeout_job_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "regia_token_digest"
    t.datetime "regia_token_expires_at"
    t.index ["match_id"], name: "index_stream_sessions_on_match_id"
    t.index ["publish_token"], name: "index_stream_sessions_on_publish_token", unique: true
    t.index ["regia_token_digest"], name: "index_stream_sessions_on_regia_token_digest", unique: true
    t.index ["status"], name: "index_stream_sessions_on_status"
    t.index ["user_id"], name: "index_stream_sessions_on_user_id"
  end

  create_table "subscriptions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "plan_id", null: false
    t.string "status", default: "active", null: false
    t.string "stripe_customer_id"
    t.string "stripe_subscription_id"
    t.datetime "current_period_start"
    t.datetime "current_period_end"
    t.boolean "cancel_at_period_end", default: false, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.uuid "club_id", null: false
    t.string "billing_interval"
    t.index ["club_id"], name: "index_subscriptions_on_club_id", unique: true
    t.index ["plan_id"], name: "index_subscriptions_on_plan_id"
    t.index ["stripe_subscription_id"], name: "index_subscriptions_on_stripe_subscription_id", unique: true, where: "(stripe_subscription_id IS NOT NULL)"
  end

  create_table "team_invitations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "team_id", null: false
    t.string "email", null: false
    t.string "token_digest", null: false
    t.string "role", default: "member", null: false
    t.datetime "expires_at", null: false
    t.datetime "accepted_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "staff_kind", default: "transmission", null: false
    t.index ["team_id", "email"], name: "index_team_invitations_on_team_id_and_email"
    t.index ["team_id", "staff_kind"], name: "index_team_invitations_on_team_id_and_staff_kind"
    t.index ["team_id"], name: "index_team_invitations_on_team_id"
    t.index ["token_digest"], name: "index_team_invitations_on_token_digest", unique: true
  end

  create_table "team_roster_members", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "team_id", null: false
    t.string "category", default: "player", null: false
    t.string "full_name", null: false
    t.string "role_label"
    t.integer "jersey_number"
    t.text "bio"
    t.integer "position", default: 0, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["team_id", "category", "position"], name: "index_team_roster_members_on_team_id_and_category_and_position"
    t.index ["team_id"], name: "index_team_roster_members_on_team_id"
  end

  create_table "teams", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name", null: false
    t.string "sport", default: "volleyball"
    t.string "logo_url"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.uuid "club_id", null: false
    t.string "primary_color"
    t.string "secondary_color"
    t.text "description"
    t.index ["club_id"], name: "index_teams_on_club_id"
  end

  create_table "user_teams", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "user_id", null: false
    t.uuid "team_id", null: false
    t.string "role", default: "member", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "staff_kind"
    t.index ["team_id", "staff_kind"], name: "index_user_teams_on_team_id_and_staff_kind"
    t.index ["team_id"], name: "index_user_teams_on_team_id"
    t.index ["user_id", "team_id"], name: "index_user_teams_on_user_id_and_team_id", unique: true
    t.index ["user_id"], name: "index_user_teams_on_user_id"
  end

  create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "email", null: false
    t.string "password_digest", null: false
    t.string "name", null: false
    t.string "role", default: "coach", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "password_reset_digest"
    t.datetime "password_reset_sent_at"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["password_reset_digest"], name: "index_users_on_password_reset_digest", unique: true
  end

  create_table "youtube_credentials", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "team_id", null: false
    t.text "access_token_encrypted"
    t.text "refresh_token_encrypted"
    t.datetime "expires_at"
    t.string "channel_id"
    t.string "channel_title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["team_id"], name: "index_youtube_credentials_on_team_id"
  end

  add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
  add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
  add_foreign_key "billing_invoices", "billing_payments"
  add_foreign_key "billing_invoices", "clubs"
  add_foreign_key "billing_payments", "clubs"
  add_foreign_key "club_memberships", "clubs"
  add_foreign_key "club_memberships", "users"
  add_foreign_key "device_states", "stream_sessions"
  add_foreign_key "matches", "teams"
  add_foreign_key "recordings", "stream_sessions"
  add_foreign_key "recordings", "teams"
  add_foreign_key "score_states", "stream_sessions"
  add_foreign_key "stream_events", "stream_sessions"
  add_foreign_key "stream_sessions", "matches"
  add_foreign_key "stream_sessions", "users"
  add_foreign_key "subscriptions", "clubs"
  add_foreign_key "subscriptions", "plans"
  add_foreign_key "team_invitations", "teams"
  add_foreign_key "team_roster_members", "teams"
  add_foreign_key "teams", "clubs"
  add_foreign_key "user_teams", "teams"
  add_foreign_key "user_teams", "users"
  add_foreign_key "youtube_credentials", "teams"
end
