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

interface Input { keyword?: string; building?: string; status?: string; limit?: number }
@Injectable()
export class SearchRoomsTool implements ToolDef<Input> {
  readonly name = 'search_rooms'; readonly skillKey = 'dormitory'; readonly requiredPermission = 'room:view';
  readonly description = '查询宿舍及床位占用数量，不返回住户资料。';
  readonly inputSchema = { type: 'object', properties: { keyword: { type: 'string', maxLength: 50 }, building: { type: 'string', maxLength: 50 }, status: { type: 'string', maxLength: 20 }, limit: { type: 'integer', minimum: 1, maximum: 50 } }, additionalProperties: false };
  constructor(private readonly service: RoomsService) {}
  validate(raw: Record<string, unknown>): ToolInputResult<Input> {
    const invalid = rejectUnknownKeys(raw, ['keyword', 'building', 'status', 'limit']); if (invalid) return invalid;
    const keyword = optionalString(raw.keyword, 'keyword', 50); if (!keyword.ok) return keyword;
    const building = optionalString(raw.building, 'building', 50); if (!building.ok) return building;
    const status = optionalString(raw.status, 'status', 20); if (!status.ok) return status;
    const limit = optionalPositiveInt(raw.limit, 'limit', 50); if (!limit.ok) return limit;
    return { ok: true, value: { keyword: keyword.value, building: building.value, status: status.value, limit: limit.value } };
  }
  execute(input: Input, _context: AgentToolContext) { return this.service.agentSearchRooms(input); }
}
