export type RawMaterial = {
  id: number;
  tenant_id?: string;
  name: string;
  supplier?: string | null;
  unit_of_measure: string;
  kg_per_unit: number;
  status: string;
  notes?: string | null;
  created_at?: string;
  current_price?: {
    id?: number;
    market_value: number;
    waste_percentage: number;
    cost_per_kg: number;
    effective_date: string;
    status?: string;
    notes?: string | null;
    created_at?: string;
    loss_cost?: number;
    cost_per_unit?: number;
  } | null;
};

export type MixIngredient = {
  id: number;
  raw_material_id: number;
  raw_material_name: string;
  quantity_kg: number;
  cost_per_kg: number | null;
  line_cost: number | null;
  notes?: string | null;
};

export type Mix = {
  id: number;
  tenant_id?: string;
  client_name: string;
  name: string;
  status: string;
  version?: number;
  notes?: string | null;
  created_at?: string;
  ingredients: MixIngredient[];
  total_mix_kg: number;
  total_mix_cost: number;
  mix_cost_per_kg: number | null;
  warnings: string[];
};

export type MixIngredientInput = {
  raw_material_id: number;
  quantity_kg: number;
  notes?: string | null;
};

export type MixCreateInput = {
  client_name: string;
  name: string;
  status?: string;
  version?: number;
  notes?: string | null;
  ingredients: MixIngredientInput[];
};

export type MixUpdateInput = {
  client_name?: string;
  name?: string;
  status?: string;
  version?: number;
  notes?: string | null;
};

export type MixIngredientUpdateInput = {
  quantity_kg?: number;
  notes?: string | null;
};

export type MixCalculatorProductOption = {
  product_id: number;
  client_name: string;
  product_name: string;
  mix_id: number;
  mix_name: string;
  unit_of_measure: string;
  unit_size_kg: number;
  mix_total_kg: number;
};

export type MixCalculatorOptions = {
  clients: string[];
  products: MixCalculatorProductOption[];
};

export type MixCalculatorLine = {
  id?: number | null;
  raw_material_id?: number | null;
  raw_material_name: string;
  required_kg: number;
  mix_percentage: number;
  unit: string;
  sort_order: number;
};

export type MixCalculatorPreview = {
  client_name: string;
  product_id: number;
  product_name: string;
  mix_id: number;
  mix_name: string;
  mix_date: string;
  batch_size_kg: number;
  total_bags: number;
  total_kg: number;
  product_unit_of_measure: string;
  product_unit_size_kg: number;
  prepared_by_name: string;
  status: string;
  notes?: string | null;
  warnings: string[];
  lines: MixCalculatorLine[];
};

export type MixCalculatorSessionSummary = {
  id: number;
  tenant_id: string;
  session_number: string;
  client_name: string;
  product_id: number;
  product_name: string;
  mix_id: number;
  mix_name: string;
  mix_date: string;
  batch_size_kg: number;
  total_bags: number;
  total_kg: number;
  product_unit_of_measure: string;
  product_unit_size_kg: number;
  prepared_by_user_id?: number | null;
  prepared_by_name: string;
  created_by: string;
  status: string;
  notes?: string | null;
  created_at: string;
  updated_at: string;
  warnings: string[];
  is_owner: boolean;
};

export type MixCalculatorSession = MixCalculatorSessionSummary & {
  lines: MixCalculatorLine[];
};

export type MixCalculatorCreateInput = {
  mix_date: string;
  client_name: string;
  product_id: number;
  batch_size_kg: number;
  prepared_by_name: string;
  status?: string;
  notes?: string | null;
};

export type MixCalculatorUpdateInput = {
  mix_date?: string;
  client_name?: string;
  product_id?: number;
  batch_size_kg?: number;
  prepared_by_name?: string;
  status?: string;
  notes?: string | null;
};

export type Product = {
  id: number;
  tenant_id?: string;
  name: string;
  client_name: string;
  mix_id?: number;
  mix_name: string;
  sale_type: string;
  own_bag?: boolean;
  unit_of_measure: string;
  items_per_pallet?: number;
  bagging_process?: string | null;
  distributor_margin: number | null;
  wholesale_margin: number | null;
  notes?: string | null;
  created_at?: string;
};

export type ProductCostBreakdown = {
  product_id: number;
  product_name: string;
  client_name: string;
  mix_name: string;
  cleaned_product_cost?: number;
  grading_cost?: number;
  bagging_cost?: number;
  cracking_cost?: number;
  bag_cost?: number;
  freight_cost?: number;
  finished_product_delivered: number;
  distributor_price: number | null;
  wholesale_price: number | null;
  warnings: string[];
  inputs?: Record<string, unknown>;
};

export type Scenario = {
  id: number;
  name: string;
  status: string;
  description?: string | null;
  overrides: Record<string, unknown>;
};

export type ClientAccessUser = {
  id: number;
  client_account_id: number;
  full_name: string;
  email: string;
  role: string;
  status: string;
  is_new_user: boolean;
  last_login_at?: string | null;
  created_at: string;
  module_permissions: ClientUserModulePermission[];
};

export type ClientUserModulePermission = {
  id: number;
  client_account_id: number;
  client_user_id: number;
  module_key: string;
  module_name: string;
  module_group: string;
  description?: string | null;
  access_level: string;
  updated_at: string;
  created_at: string;
};

export type ClientAccessFeature = {
  id: number;
  client_account_id: number;
  feature_key: string;
  feature_name: string;
  feature_group: string;
  description?: string | null;
  enabled: boolean;
  updated_at: string;
  created_at: string;
};

export type ClientAccessAccount = {
  id: number;
  tenant_id: string;
  name: string;
  client_code: string;
  status: string;
  powerbi_workspace?: string | null;
  notes?: string | null;
  created_at: string;
  users: ClientAccessUser[];
  features: ClientAccessFeature[];
  active_user_count: number;
  new_user_count: number;
  enabled_feature_count: number;
  total_feature_count: number;
  audit_history: ClientAccessAuditEvent[];
};

export type ClientAccessAuditEvent = {
  id: number;
  client_account_id: number;
  actor_type: string;
  actor_name: string;
  actor_email: string;
  actor_role: string;
  action: string;
  target_type: string;
  target_id?: number | null;
  module_key?: string | null;
  summary: string;
  created_at: string;
};

export type ClientAccessExportRow = Record<string, unknown>;

export type ClientAccessPowerBiExport = {
  generated_at: string;
  client_rows: ClientAccessExportRow[];
  user_rows: ClientAccessExportRow[];
  feature_rows: ClientAccessExportRow[];
  permission_rows: ClientAccessExportRow[];
  audit_rows: ClientAccessExportRow[];
  clients: ClientAccessAccount[];
};

export type DashboardSummary = {
  raw_materials: {
    count: number;
    total_market_value: number;
    latest: { id: number; name: string; market_value: number; cost_per_kg: number; effective_date: string | null } | null;
  } | null;
  mixes: {
    count: number;
    average_cost_per_kg: number;
    top: {
      id: number;
      name: string;
      client_name: string;
      ingredients_count: number;
      total_mix_kg: number;
      total_mix_cost: number;
      mix_cost_per_kg: number | null;
      warnings: string[];
    } | null;
  } | null;
  products: {
    count: number;
    top: { id: number; product_name: string; client_name: string; finished_product_delivered: number; warnings: string[] } | null;
    top_products: Array<{ id: number; product_name: string; client_name: string; finished_product_delivered: number; warnings: string[] }>;
  } | null;
  trend_seeds: {
    raw_material_cost_per_kg: number[];
    mix_cost_per_kg: number[];
    product_finished_delivered: number[];
  };
};

export type LoginResponse = {
  name: string;
  email: string;
  role: string;
  token: string;
  tenant_id?: string | null;
  client_role?: string | null;
  user_id?: number | null;
  client_account_id?: number | null;
  module_permissions?: Record<string, string>;
  // Permission-key array populated by the internal Hunter Stock Feeds login.
  // Drives permission-based UI gating; legacy client logins leave it undefined.
  permissions?: string[];
  // Display-friendly role label (e.g. "Admin", "Operations") when role === 'internal'.
  role_name?: string | null;
};

export type RawMaterialCreateInput = {
  name: string;
  supplier?: string | null;
  unit_of_measure: string;
  kg_per_unit: number;
  status?: string;
  notes?: string | null;
  initial_price: {
    market_value: number;
    waste_percentage: number;
    effective_date: string;
    status?: string;
    notes?: string | null;
  };
};

export type RawMaterialPriceCreateInput = {
  market_value: number;
  waste_percentage: number;
  effective_date: string;
  status?: string;
  notes?: string | null;
};

export type ClientUserCreateInput = {
  client_account_id: number;
  full_name: string;
  email: string;
  role?: string;
  status?: string;
  is_new_user?: boolean;
};

export type ClientUserUpdateInput = {
  full_name?: string;
  email?: string;
  role?: string;
  status?: string;
  is_new_user?: boolean;
};
