/**
 * Daily Controller
 *
 * REST endpoints for accounting entries (daily_tr)
 * Replaces: LastACDaily, HoldACDaily COM methods
 */
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { DailyService } from './daily.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('daily')
@UseGuards(JwtAuthGuard)
export class DailyController {
  constructor(private dailyService: DailyService) {}

  /**
   * GET /api/daily/last/:kind/:ref
   * Maps to: LastACDaily(kind, ref)
   */
  @Get('last/:kind/:ref')
  async getLast(
    @Param('kind') kind: string,
    @Param('ref') ref: string,
  ) {
    const num = await this.dailyService.getLastACDailyNo(
      parseInt(kind, 10),
      parseInt(ref, 10),
    );
    return { lastACDaily: num };
  }

  /**
   * GET /api/daily/hold/:kind/:ref
   * Maps to: HoldACDaily(kind, ref)
   */
  @Get('hold/:kind/:ref')
  async hold(
    @Param('kind') kind: string,
    @Param('ref') ref: string,
  ) {
    const count = await this.dailyService.holdACDaily(
      parseInt(kind, 10),
      parseInt(ref, 10),
    );
    return { holdACDaily: count };
  }

  /**
   * GET /api/daily/mg/last/:kind/:ref
   * Maps to: LastMGDaily(kind, ref)
   */
  @Get('mg/last/:kind/:ref')
  async getLastMg(
    @Param('kind') kind: string,
    @Param('ref') ref: string,
  ) {
    const num = await this.dailyService.getLastMGDailyNo(
      parseInt(kind, 10),
      parseInt(ref, 10),
    );
    return { lastMGDaily: num };
  }

  /**
   * GET /api/daily/mg/hold/:kind/:ref
   * Maps to: HoldMGDaily(kind, ref)
   */
  @Get('mg/hold/:kind/:ref')
  async holdMg(
    @Param('kind') kind: string,
    @Param('ref') ref: string,
  ) {
    const count = await this.dailyService.holdMGDaily(
      parseInt(kind, 10),
      parseInt(ref, 10),
    );
    return { holdMGDaily: count };
  }

  /**
   * GET /api/daily/entries/:kind/:ref
   * Get entries for kind/ref, optional tranNo filter
   */
  @Get('entries/:kind/:ref')
  async getEntries(
    @Param('kind') kind: string,
    @Param('ref') ref: string,
    @Query('tranNo') tranNo?: string,
  ) {
    const k = parseInt(kind, 10);
    const r = parseInt(ref, 10);
    const tn = tranNo ? parseInt(tranNo, 10) : undefined;
    return this.dailyService.getEntries(k, r, tn);
  }
}
