/**
 * シルギア 割増賃金・労働時間API v1 の型定義
 * https://api.shirugear.com/rodo/
 *
 * 労働基準法で決まっている割増賃金の率（時間外・月60時間超・休日・深夜とその組み合わせ）、
 * 割増賃金の基礎から除く手当、法定労働時間・休憩・法定休日、36協定の上限を返します。
 * 率は法定の最低限で、0.25 のような小数です。時間は「時間」の数です。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

export type RateKey = "overtime" | "overtime_over_60h" | "holiday" | "night";

export interface Rate {
  key: RateKey;
  label: string;
  /** 法定の最低限の割増率（0.25 = 25%） */
  rate: number;
  law: string;
}

export interface Combination {
  key: "overtime_night" | "overtime_over_60h_night" | "holiday_night" | "holiday_over_8h";
  label: string;
  /** 足し合わせる率の key */
  parts: RateKey[];
  /** 合わせた率（parts の率の合計） */
  rate: number;
  note?: string;
  law: string;
}

export interface Applies {
  company: "large" | "sme";
  label: string;
  /** この日から当てはまる */
  from: string;
  before?: string;
}

export interface Premium {
  name: string;
  rates: Rate[];
  combinations: Combination[];
  /** 深夜の時間帯 */
  night: { from: "22:00"; until: "05:00"; alt: { from: string; until: string; note: string }; managers: string; law: string };
  /** 月60時間を超える時間外労働 */
  over_60h: { threshold_hours: number; applies: Applies[]; month: string; holiday: string; substitute_leave: string; law: string };
  /** 中小企業の範囲（旧第138条。資本金か労働者数のどちらかに当てはまれば中小企業） */
  sme: {
    rule: string;
    by_industry: { industry: "retail" | "service" | "wholesale" | "other"; label: string; capital_max: number; workers_max: number }[];
    law: string;
  };
  /** 割増賃金の基礎（1時間あたりの賃金の出し方と、除く手当） */
  base_wage: {
    rule: string;
    hourly: { unit: "hour" | "day" | "week" | "month" | "piece" | "mixed"; label: string; rule: string }[];
    excluded: { key: string; label: string; since?: string }[];
    excluded_note: string;
    law: string;
  };
  data_version: string;
}

export interface Limits {
  /** 特別条項の時間外労働の年の上限。null はその決まりが当てはまらない */
  year_overtime_max: number | null;
  /** 時間外と休日労働の合計の、1か月の上限（この値「未満」） */
  month_total_lt: number | null;
  /** 2〜6か月の平均の上限（この値「以内」） */
  avg_total_max: number | null;
  /** 月45時間を超えられる月数の上限 */
  months_over_limit_max: number | null;
}

export interface Industry extends Limits {
  key: "construction" | "driving" | "doctor" | "sugar";
  label: string;
  from: string;
  limit: { month: number; year: number };
  rule: string;
  disaster?: { month_total_lt: null; avg_total_max: null };
  levels?: { level: "A" | "renkei_B" | "B" | "C"; label?: string; agreement_year_max: number; personal_year_max: number; personal_month_lt: number }[];
  law: string;
}

export interface Hours {
  name: string;
  statutory: { day_hours: 8; week_hours: 40; law: string };
  special_week: { week_hours: 44; day_hours: 8; workers_lt: 10; industries: { no: number; label: string }[]; rule: string; law: string };
  break: { rules: { over_hours: number; min_minutes: number }[]; note: string; law: string };
  holiday: { per_week: 1; per_4_weeks: 4; note: string; law: string };
  agreement36: {
    limit: { month: number; year: number };
    limit_variable_1year: { month: number; year: number; note: string };
    special: { year_overtime_max: number; month_total_lt: number; avg_months: number[]; avg_total_max: number; months_over_limit_max: number; note: string };
    always: string;
    harmful_daily_max: number;
    harmful_note: string;
    exempt: string;
    applies: Applies[];
    law: string;
  };
  industries: Industry[];
  industries_before: string;
  exempt: { rule: string; law: string };
  data_version: string;
}

export interface Meta {
  data_version: string;
  checked_at: string;
  files: ("premium" | "hours")[];
  /** 照合した法令の版（施行日） */
  law: { labor_standards_act: string; enforcement_regulations: string; premium_order: string };
  scope: string;
  sources: { label: string; url: string }[];
}

export interface ApiError {
  error: {
    status: number;
    code: "invalid_request" | "not_found" | "region_not_allowed" | "method_not_allowed";
    message: string;
  };
}
