/**
 * Database type definitions for the typed PostgREST client (`src/lib/api.ts`).
 *
 * The shape mirrors the standard Supabase-generated `Database` type so call sites
 * that previously used the SDK can swap to the new client with no behavior
 * change: `api.from("brands")` returns a `QueryBuilder<BrandRow>` and the
 * `await` resolves to `{ data: BrandRow[], error: null }`.
 *
 * Tight types are hand-curated for hot tables used by client components
 * (brands, admin_users, stops, orders, products, order_items, brand_settings).
 * Every other table falls back to `Record<string, unknown>` so the client
 * works against any table name without a TS error — call sites can still
 * cast: `const { data } = await api.from<MyType>("custom_table")`.
 *
 * If a column is added to a hot table, update the corresponding `Row` here.
 * For non-hot tables, `Record<string, unknown>` is fine for now; tighten
 * as needed.
 */

// ── Hot tables — typed row shapes ────────────────────────────────────────────

export type BrandRow = {
  id: string;
  name: string;
  slug: string;
  logo_url: string | null;
  primary_color: string | null;
  accent_color: string | null;
  hero_image_url: string | null;
  plan_tier: "starter" | "farm" | "enterprise" | string | null;
  max_users: number | null;
  max_stops_monthly: number | null;
  max_products: number | null;
  stripe_customer_id: string | null;
  created_at: string;
  updated_at: string;
  [k: string]: any;
};

export type AdminUserRow = {
  id: string;
  user_id: string | null;
  brand_id: string | null;
  role: "platform_admin" | "brand_admin" | "store_employee" | "staff" | string;
  can_manage_products: boolean | null;
  can_manage_stops: boolean | null;
  can_manage_orders: boolean | null;
  can_manage_pickup: boolean | null;
  can_manage_messages: boolean | null;
  can_manage_refunds: boolean | null;
  can_manage_users: boolean | null;
  can_manage_water_log: boolean | null;
  can_manage_reports: boolean | null;
  can_manage_settings: boolean | null;
  created_at: string | null;
  active: boolean;
  last_login: string | null;
  must_change_password: boolean;
  password_changed_at: string | null;
  temp_password_set_at: string | null;
  phone_number: string | null;
  display_name: string | null;
  [k: string]: any;
};

export type StopRow = {
  id: string;
  brand_id: string;
  name: string | null;
  city: string | null;
  state: string | null;
  address: string | null;
  date: string;
  time: string | null;
  location: string | null;
  slug: string | null;
  status: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
  [k: string]: any;
};

export type OrderRow = {
  id: string;
  brand_id: string;
  stop_id: string | null;
  customer_email: string | null;
  customer_name: string | null;
  customer_phone: string | null;
  customer_address: string | null;
  status: string;
  total: number | null;
  subtotal: number | null;
  tax: number | null;
  payment_status: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
  [k: string]: any;
};

export type OrderItemRow = {
  id: string;
  order_id: string;
  product_id: string | null;
  product_name: string | null;
  quantity: number;
  unit_price: number | null;
  subtotal: number | null;
  fulfillment: "pickup" | "ship" | string;
  [k: string]: any;
};

export type ProductRow = {
  id: string;
  brand_id: string;
  name: string;
  description: string | null;
  price: number;
  type: string | null;
  image_url: string | null;
  is_taxable: boolean | null;
  pickup_type: "scheduled_stop" | "shed" | string | null;
  in_stock: boolean | null;
  inventory: number | null;
  square_id: string | null;
  created_at: string;
  updated_at: string;
  [k: string]: any;
};

export type BrandSettingsRow = {
  id: string;
  brand_id: string;
  hero_tagline: string | null;
  hero_subhead: string | null;
  about_headline: string | null;
  about_body: string | null;
  contact_email: string | null;
  contact_phone: string | null;
  feature_flags: Record<string, boolean> | null;
  updated_at: string;
  [k: string]: any;
};

// ── The Database shape — one entry per public table ─────────────────────────

export type TableSchema<TRow = Record<string, unknown>> = {
  Row: TRow;
  Insert: Partial<TRow>;
  Update: Partial<TRow>;
};

export type Database = {
  public: {
    Tables: {
      // Hot tables — typed rows
      brands: TableSchema<BrandRow>;
      admin_users: TableSchema<AdminUserRow>;
      stops: TableSchema<StopRow>;
      orders: TableSchema<OrderRow>;
      order_items: TableSchema<OrderItemRow>;
      products: TableSchema<ProductRow>;
      brand_settings: TableSchema<BrandSettingsRow>;
      // Fallback for every other table — call sites pass an explicit generic
      // (e.g. `api.from<MyType>("my_table")`) or get `Record<string, unknown>`.
      [table: string]: TableSchema<any>;
    };
    Views: { [view: string]: { Row: Record<string, unknown> } };
    Functions: { [fn: string]: { Args: Record<string, unknown>; Returns: unknown } };
  };
};

/** Resolves the Row type for a given table, falling back to a permissive shape. */
export type RowOf<TTable extends keyof Database["public"]["Tables"] | string> =
  TTable extends keyof Database["public"]["Tables"]
    ? Database["public"]["Tables"][TTable]["Row"]
    : Record<string, unknown>;
