export type TerrainType = 'plain' | 'road' | 'forest' | 'hill' | 'village' | 'fort' | 'camp' | 'river' | 'cliff';

export type UnitClassKey =
  | 'lord'
  | 'infantry'
  | 'cavalry'
  | 'spearman'
  | 'archer'
  | 'strategist'
  | 'quartermaster'
  | 'bandit'
  | 'yellowTurban'
  | 'rebelLeader';

export type TerrainRule = {
  label: string;
  color: number;
  alpha: number;
  moveCost: number;
  defenseBonus: number;
  passable?: boolean;
  recoveryHp?: number;
  moraleBonus?: number;
};

export type UnitClassDefinition = {
  key: UnitClassKey;
  name: string;
  family: string;
  role: string;
  description: string;
  terrainRatings: Record<TerrainType, number>;
  movementCosts?: Partial<Record<TerrainType, number>>;
};

export const terrainRules: Record<TerrainType, TerrainRule> = {
  plain: { label: '평지', color: 0x6d8056, alpha: 0.01, moveCost: 1, defenseBonus: 0 },
  road: { label: '길', color: 0xb08b55, alpha: 0.015, moveCost: 1, defenseBonus: 0 },
  forest: { label: '숲', color: 0x355f42, alpha: 0.025, moveCost: 2, defenseBonus: 10 },
  hill: { label: '언덕', color: 0x82724e, alpha: 0.025, moveCost: 2, defenseBonus: 8 },
  village: { label: '마을', color: 0x8d6f4b, alpha: 0.03, moveCost: 1, defenseBonus: 12, recoveryHp: 8, moraleBonus: 3 },
  fort: { label: '요새', color: 0x735c46, alpha: 0.035, moveCost: 1, defenseBonus: 18, recoveryHp: 10, moraleBonus: 4 },
  camp: { label: '진영', color: 0x4f6a73, alpha: 0.03, moveCost: 1, defenseBonus: 15, recoveryHp: 6, moraleBonus: 4 },
  river: { label: '강', color: 0x2c6687, alpha: 0.035, moveCost: 99, defenseBonus: -4, passable: false },
  cliff: { label: '절벽', color: 0x39342e, alpha: 0.045, moveCost: 99, defenseBonus: 20, passable: false }
};

export const unitClasses: Record<UnitClassKey, UnitClassDefinition> = {
  lord: {
    key: 'lord',
    name: '군웅',
    family: '군주계',
    role: '지휘 / 균형',
    description: '지형을 크게 타지 않고 아군 전선을 안정시키는 지휘관 병과.',
    terrainRatings: {
      plain: 100,
      road: 105,
      forest: 95,
      hill: 95,
      village: 110,
      fort: 115,
      camp: 115,
      river: 0,
      cliff: 0
    }
  },
  infantry: {
    key: 'infantry',
    name: '의용 보병',
    family: '보병계',
    role: '전열 / 방어',
    description: '숲과 진영에서 버티는 힘이 좋고 기병의 돌격을 받아내기 좋다.',
    terrainRatings: {
      plain: 100,
      road: 100,
      forest: 110,
      hill: 105,
      village: 115,
      fort: 120,
      camp: 115,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      forest: 1,
      village: 1,
      fort: 1,
      camp: 1
    }
  },
  cavalry: {
    key: 'cavalry',
    name: '경기병',
    family: '기병계',
    role: '기동 / 돌파',
    description: '길과 평지에서 빠르게 압박하지만 숲과 언덕에서는 힘이 떨어진다.',
    terrainRatings: {
      plain: 110,
      road: 120,
      forest: 75,
      hill: 80,
      village: 95,
      fort: 90,
      camp: 100,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      road: 1,
      forest: 3,
      hill: 3
    }
  },
  spearman: {
    key: 'spearman',
    name: '창병',
    family: '창병계',
    role: '대기병 / 압박',
    description: '기병 견제와 전선 고정에 특화된 근접 병과.',
    terrainRatings: {
      plain: 105,
      road: 100,
      forest: 95,
      hill: 105,
      village: 105,
      fort: 110,
      camp: 110,
      river: 0,
      cliff: 0
    }
  },
  archer: {
    key: 'archer',
    name: '궁병',
    family: '궁병계',
    role: '원거리 / 견제',
    description: '언덕과 요새에서 유리하고 근접 압박에는 약한 원거리 병과.',
    terrainRatings: {
      plain: 95,
      road: 95,
      forest: 90,
      hill: 115,
      village: 100,
      fort: 120,
      camp: 105,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      hill: 1
    }
  },
  strategist: {
    key: 'strategist',
    name: '책사',
    family: '책사계',
    role: '책략 / 보조',
    description: '직접 전투력은 낮지만 책략과 지휘 보조로 전장의 흐름을 바꾸는 병과.',
    terrainRatings: {
      plain: 95,
      road: 100,
      forest: 95,
      hill: 105,
      village: 110,
      fort: 115,
      camp: 115,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      hill: 1,
      village: 1,
      fort: 1,
      camp: 1
    }
  },
  quartermaster: {
    key: 'quartermaster',
    name: '군량관',
    family: '보급계',
    role: '회복 / 보급',
    description: '군량과 보급로를 관리해 장기전에 강하고 마을과 진영에서 안정적인 병과.',
    terrainRatings: {
      plain: 95,
      road: 105,
      forest: 90,
      hill: 90,
      village: 115,
      fort: 105,
      camp: 120,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      road: 1,
      village: 1,
      fort: 1,
      camp: 1
    }
  },
  bandit: {
    key: 'bandit',
    name: '도적',
    family: '적병계',
    role: '습격 / 산악',
    description: '숲과 언덕에서 강하고 정규전 방어 지형에서는 약하다.',
    terrainRatings: {
      plain: 95,
      road: 95,
      forest: 115,
      hill: 115,
      village: 100,
      fort: 90,
      camp: 95,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      forest: 1,
      hill: 1
    }
  },
  yellowTurban: {
    key: 'yellowTurban',
    name: '황건병',
    family: '황건적계',
    role: '민병 / 물량',
    description: '능력은 낮지만 험지에서 성가시게 버티는 반란군 병과.',
    terrainRatings: {
      plain: 95,
      road: 95,
      forest: 105,
      hill: 105,
      village: 100,
      fort: 90,
      camp: 95,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      forest: 1
    }
  },
  rebelLeader: {
    key: 'rebelLeader',
    name: '두령',
    family: '두령계',
    role: '지휘 / 근접',
    description: '황건적을 이끄는 지휘관형 적병. 진영과 험지에서 버티는 힘이 있다.',
    terrainRatings: {
      plain: 100,
      road: 100,
      forest: 110,
      hill: 110,
      village: 105,
      fort: 105,
      camp: 110,
      river: 0,
      cliff: 0
    },
    movementCosts: {
      forest: 1,
      hill: 1
    }
  }
};

export function getUnitClass(classKey: UnitClassKey) {
  return unitClasses[classKey];
}

export function getTerrainRule(terrain: TerrainType) {
  return terrainRules[terrain];
}
