export interface OfficialKyosaiField {
  id: number;
  k_num: string;
  s_num: string;
  address: string;
  kanji_name: string;
  area: number;
  linked_field_names?: string[];
}

export interface OfficialChusankanField {
  id: number;
  c_id: string;
  oaza: string;
  aza: string;
  chiban: string;
  area: number;
  payment_amount: number | null;
  linked_field_names?: string[];
}

export interface Field {
  id: number;
  name: string;
  address: string;
  area_tan: string;
  area_m2: number;
  owner_name: string;
  group_name: string | null;
  display_order: number;
  kyosai_fields: OfficialKyosaiField[];
  chusankan_fields: OfficialChusankanField[];
}

export interface Variety {
  id: number;
  crop: number;
  name: string;
  default_seedling_boxes_per_tan: string;
  seed_material: number | null;
  seed_material_name: string | null;
}

export interface Crop {
  id: number;
  name: string;
  varieties: Variety[];
}

export interface Plan {
  id: number;
  field: number;
  field_name: string;
  year: number;
  crop: number;
  crop_name: string;
  variety: number;
  variety_name: string;
  notes: string | null;
  variety_change_count?: number;
  latest_variety_change?: {
    id: number;
    changed_at: string;
    old_variety_id: number | null;
    old_variety_name: string | null;
    new_variety_id: number | null;
    new_variety_name: string | null;
    fertilizer_moved_entry_count: number;
  } | null;
}

export interface Fertilizer {
  id: number;
  name: string;
  maker: string | null;
  capacity_kg: string | null;
  nitrogen_pct: string | null;
  phosphorus_pct: string | null;
  potassium_pct: string | null;
  notes: string | null;
  material: number | null;
  material_id: number | null;
}

export interface FertilizerProfile {
  capacity_kg: string | null;
  nitrogen_pct: string | null;
  phosphorus_pct: string | null;
  potassium_pct: string | null;
}

export interface PesticideProfile {
  registration_no: string;
  formulation: string;
  usage_unit: string;
  dilution_ratio: string;
  active_ingredient: string;
  category: string;
}

export interface Material {
  id: number;
  name: string;
  material_type: 'fertilizer' | 'pesticide' | 'seed' | 'seedling' | 'other';
  material_type_display: string;
  maker: string;
  stock_unit: 'bag' | 'bottle' | 'kg' | 'liter' | 'piece';
  stock_unit_display: string;
  is_active: boolean;
  notes: string;
  fertilizer_profile: FertilizerProfile | null;
  pesticide_profile: PesticideProfile | null;
  current_stock: string;
  created_at: string;
  updated_at: string;
}

export interface StockTransaction {
  id: number;
  material: number;
  material_name: string;
  material_type: string;
  transaction_type: 'purchase' | 'use' | 'reserve' | 'adjustment_plus' | 'adjustment_minus' | 'discard';
  transaction_type_display: string;
  quantity: string;
  stock_unit: string;
  stock_unit_display: string;
  occurred_on: string;
  note: string;
  fertilization_plan: number | null;
  spreading_item?: number | null;
  is_locked: boolean;
  created_at: string;
}

export interface StockSummary {
  material_id: number;
  name: string;
  material_type: 'fertilizer' | 'pesticide' | 'seed' | 'seedling' | 'other';
  material_type_display: string;
  maker: string;
  stock_unit: string;
  stock_unit_display: string;
  is_active: boolean;
  current_stock: string;
  reserved_stock: string;
  available_stock: string;
  last_transaction_date: string | null;
}

export interface FertilizationEntry {
  id?: number;
  field: number;
  field_name?: string;
  field_area_tan?: string;
  fertilizer: number;
  fertilizer_name?: string;
  bags: number | string;
  actual_bags?: string | null;
}

export interface FertilizationPlan {
  id: number;
  name: string;
  year: number;
  variety: number;
  variety_name: string;
  crop_name: string;
  calc_settings: { fertilizer_id: number; method: string; param: string }[];
  entries: FertilizationEntry[];
  field_count: number;
  fertilizer_count: number;
  planned_total_bags: string;
  spread_total_bags: string;
  remaining_total_bags: string;
  spread_status: 'unspread' | 'partial' | 'completed' | 'over_applied';
  is_confirmed: boolean;
  confirmed_at: string | null;
  is_variety_change_plan: boolean;
  created_at: string;
  updated_at: string;
}

export interface RiceTransplantEntry {
  id?: number;
  field: number;
  field_name?: string;
  field_area_tan?: string;
  installed_seedling_boxes: string;
  default_seedling_boxes: string;
  planned_boxes: string;
}

export interface RiceTransplantPlan {
  id: number;
  name: string;
  year: number;
  variety: number;
  variety_name: string;
  crop_name: string;
  default_seed_grams_per_box: string;
  seedling_boxes_per_tan: string;
  notes: string;
  seed_material_name: string | null;
  entries: RiceTransplantEntry[];
  field_count: number;
  total_seedling_boxes: string;
  total_seed_kg: string;
  variety_seed_inventory_kg: string;
  remaining_seed_kg: string;
  created_at: string;
  updated_at: string;
}

export interface DeliveryGroupField {
  id: number;
  name: string;
  area_tan: string;
}

export interface DeliveryGroup {
  id: number;
  name: string;
  order: number;
  fields: DeliveryGroupField[];
}

export interface DeliveryTripItem {
  id: number;
  field: number;
  field_name: string;
  fertilizer: number;
  fertilizer_name: string;
  bags: string;
  spread_bags: string;
  remaining_bags: string;
}

export interface DeliveryTrip {
  id: number;
  order: number;
  name: string;
  date: string | null;
  work_record_id: number | null;
  items: DeliveryTripItem[];
}

export interface DeliveryAllEntry {
  field: number;
  field_name: string;
  field_area_tan: string;
  fertilizer: number;
  fertilizer_name: string;
  bags: string;
  actual_bags?: string | null;
}

export interface DeliveryPlan {
  id: number;
  year: number;
  name: string;
  groups: DeliveryGroup[];
  trips: DeliveryTrip[];
  unassigned_fields: DeliveryGroupField[];
  available_fertilizers: { id: number; name: string }[];
  all_entries: DeliveryAllEntry[];
  created_at: string;
  updated_at: string;
}

export interface DeliveryPlanListItem {
  id: number;
  year: number;
  name: string;
  group_count: number;
  trip_count: number;
  created_at: string;
  updated_at: string;
}

export interface SpreadingCandidate {
  field: number;
  field_name: string;
  field_area_tan: string;
  fertilizer: number;
  fertilizer_name: string;
  planned_bags: string;
  delivered_bags: string;
  spread_bags: string;
  spread_bags_other: string;
  current_session_bags: string;
  remaining_bags: string;
}

export interface SpreadingSessionItem {
  id: number;
  field: number;
  field_name: string;
  fertilizer: number;
  fertilizer_name: string;
  actual_bags: string;
  planned_bags_snapshot: string;
  delivered_bags_snapshot: string;
}

export interface SpreadingSession {
  id: number;
  year: number;
  date: string;
  name: string;
  notes: string;
  work_record_id: number | null;
  items: SpreadingSessionItem[];
  created_at: string;
  updated_at: string;
}

export interface LeveeWorkCandidate {
  field_id: number;
  field_name: string;
  field_area_tan: string;
  group_name: string | null;
  plan_id: number;
  crop_name: string;
  variety_name: string;
  selected: boolean;
}

export interface LeveeWorkSessionItem {
  id: number;
  field: number;
  field_name: string;
  field_area_tan: string;
  group_name: string | null;
  plan: number | null;
  crop_name_snapshot: string;
  variety_name_snapshot: string;
}

export interface LeveeWorkSession {
  id: number;
  year: number;
  date: string;
  title: string;
  notes: string;
  work_record_id: number | null;
  item_count: number;
  total_area_tan: string;
  items: LeveeWorkSessionItem[];
  created_at: string;
  updated_at: string;
}

export interface WorkRecord {
  id: number;
  work_date: string;
  work_type: 'fertilizer_delivery' | 'fertilizer_spreading' | 'levee_work';
  work_type_display: string;
  title: string;
  year: number;
  auto_created: boolean;
  delivery_trip: number | null;
  delivery_plan_id: number | null;
  delivery_plan_name: string | null;
  spreading_session: number | null;
  levee_work_session: number | null;
  created_at: string;
  updated_at: string;
}

export interface MailSender {
  id: number;
  type: 'address' | 'domain';
  email: string | null;
  domain: string | null;
  rule: 'never_notify' | 'always_notify';
  note: string;
  created_at: string;
}

export interface MailEmailFeedback {
  id: number;
  sender_email: string;
  sender_domain: string;
  subject: string;
  body_preview: string;
  received_at: string;
  feedback: 'important' | 'not_important' | 'never_notify' | 'always_notify' | null;
}
