export type MasterStatus = "ACTIVE" | "INACTIVE";
export type AdjustmentReasonCategory = "SHRINKAGE" | "DAMAGE" | "REGRADE" | "ADJUSTMENT";

export type GradePriceStandardRecord = {
  id: string;
  start_date: string;
  end_date: string | null;
  min_price: number;
  max_price: number;
  notes: string | null;
};

export type GradeRecord = {
  id: string;
  code: string;
  legacy_code: string | null;
  is_mangkok: boolean;
  name: string;
  description: string | null;
  status: string;
  buy_price_standards: GradePriceStandardRecord[];
  sell_price_standards: GradePriceStandardRecord[];
  created_at: string;
  updated_at: string;
};

export type WarehouseRecord = {
  id: string;
  code: string;
  name: string;
  address: string | null;
  status: string;
  created_at: string;
  updated_at: string;
};

export type WarehouseLocationRecord = {
  id: string;
  warehouse_id: string;
  warehouse_name: string;
  code: string;
  name: string;
  location_type: string | null;
  status: string;
  created_at: string;
  updated_at: string;
};

export type AdjustmentReasonRecord = {
  id: string;
  code: string;
  name: string;
  description: string | null;
  category: AdjustmentReasonCategory;
  status: string;
  created_at: string;
  updated_at: string;
};

export type UnitRecord = {
  id: string;
  code: string;
  name: string;
  created_at: string;
  updated_at: string;
};

export type CurrencyRecord = {
  id: string;
  code: string;
  name: string;
  description: string | null;
  status: string;
  created_at: string;
  updated_at: string;
};

export type EmployeeRecord = {
  id: string;
  code: string;
  name: string;
  email: string | null;
  mobile: string | null;
  position: string;
  address: string | null;
  status: string;
  created_at: string;
  updated_at: string;
};

export type CourierRecord = {
  id: string;
  code: string;
  name: string;
  address: string | null;
  phone: string | null;
  website: string | null;
  email: string | null;
  status: string;
  created_at: string;
  updated_at: string;
};

export type BankRecord = {
  id: string;
  code: string;
  name: string;
  address: string | null;
  status: string;
  created_at: string;
  updated_at: string;
};

export type ProfitShareSchemeRecord = {
  id: string;
  code: string;
  name: string;
  share_agent: number;
  share_company: number;
  status: string;
  created_at: string;
  updated_at: string;
};

export type AgentRecord = {
  id: string;
  code: string;
  name: string;
  identity_type: "KTP" | "SIM" | "PASSPORT";
  identity_number: string;
  mobile_phone: string | null;
  email: string | null;
  address: string | null;
  notes: string | null;
  join_date: string;
  profit_share_scheme: {
    id: string;
    code: string;
    name: string;
    share_agent: number;
    share_company: number;
  };
  profit_share_balance: number;
  capital_balance: number;
  bank_accounts: Array<{
    id: string;
    bank_id: string;
    bank_code: string;
    bank_name: string;
    account_number: string;
  }>;
  created_at: string;
  updated_at: string;
};

export type AgentBalanceHistoryEntry = {
  id: string;
  balance_type: "PROFIT_SHARE" | "CAPITAL";
  source:
    | "OPENING_BALANCE"
    | "MANUAL_ADJUSTMENT"
    | "CONSIGNMENT_COMMISSION"
    | "REGULAR_SALE_COMMISSION"
    | "JIT_SALE_COMMISSION"
    | "OFFICE_BUYOUT_COMMISSION"
    | "FUND_REQUEST_PROFIT_SHARE"
    | "FUND_REQUEST_CAPITAL";
  direction: "IN" | "OUT";
  amount: number;
  balance_after: number;
  occurred_at: string;
  effective_date: string | null;
  reference_no: string | null;
  description: string;
  notes: string | null;
};

export type AgentDetailRecord = AgentRecord & {
  stats: {
    purchase_count: number;
    consignment_close_count: number;
    regular_sale_close_count: number;
    history_count: number;
  };
  balance_history: AgentBalanceHistoryEntry[];
};

export type SalesRecord = {
  id: string;
  code: string;
  name: string;
  identity_type: "KTP" | "SIM" | "PASSPORT";
  identity_number: string;
  mobile_phone: string | null;
  email: string | null;
  address: string | null;
  notes: string | null;
  join_date: string;
  commission_balance: number;
  bank_accounts: Array<{
    id: string;
    bank_id: string;
    bank_code: string;
    bank_name: string;
    account_number: string;
  }>;
  created_at: string;
  updated_at: string;
};

export type SalesCommissionHistoryEntry = {
  id: string;
  source: "OPENING_BALANCE" | "MANUAL_ADJUSTMENT" | "CONSIGNMENT_COMMISSION";
  direction: "IN" | "OUT";
  amount: number;
  balance_after: number;
  occurred_at: string;
  effective_date: string | null;
  reference_no: string | null;
  description: string;
  notes: string | null;
};

export type SalesDetailRecord = SalesRecord & {
  stats: {
    consignment_count: number;
    commission_close_count: number;
    history_count: number;
  };
  commission_history: SalesCommissionHistoryEntry[];
};

export type WashingPlaceContactPersonRecord = {
  id: string;
  name: string;
  mobile_phone: string | null;
  email: string | null;
};

export type WashingPlaceRecord = {
  id: string;
  code: string;
  name: string;
  address: string | null;
  phone: string | null;
  website: string | null;
  email: string | null;
  contact_people: WashingPlaceContactPersonRecord[];
  status: string;
  created_at: string;
  updated_at: string;
};
