import { Injectable } from '@nestjs/common';
import { RoomsService } from '../../rooms/rooms.service';
import type { AgentToolContext, ToolDef, ToolInputResult } from '../agent-tool.types';
import { optionalDate, optionalPositiveInt, optionalString, rejectUnknownKeys } from './tool-input';

interface Input { date?: string; building?: string; limit?: number }
@Injectable()
export class GetRoomOccupancySummaryTool implements ToolDef<Input> {
  readonly name = 'get_room_occupancy_summary'; readonly skillKey = 'dormitory'; readonly requiredPermission = 'room:view';
  readonly description = '按日期汇总宿舍入住数量和空余床位，不返回住户资料。';
  readonly inputSchema = { type: 'object', properties: { date: { type: 'string', format: 'date' }, building: { type: 'string', maxLength: 50 }, limit: { type: 'integer', minimum: 1, maximum: 100 } }, additionalProperties: false };
  constructor(private readonly service: RoomsService) {}
  validate(raw: Record<string, unknown>): ToolInputResult<Input> {
    const invalid = rejectUnknownKeys(raw, ['date', 'building', 'limit']); if (invalid) return invalid;
    const date = optionalDate(raw.date, 'date'); if (!date.ok) return date;
    const building = optionalString(raw.building, 'building', 50); if (!building.ok) return building;
    const limit = optionalPositiveInt(raw.limit, 'limit', 100); if (!limit.ok) return limit;
    return { ok: true, value: { date: date.value, building: building.value, limit: limit.value } };
  }
  execute(input: Input, _context: AgentToolContext) { return this.service.agentGetRoomOccupancySummary(input); }
}
