import { Controller, Get, NotFoundException, Param, Res } from '@nestjs/common';
import * as path from 'path';
import * as fs from 'fs';
import { Response } from 'express';
import { SystemLang } from '@/system/lang/system.lang';
@Controller('medias')
export class MediasController {
  @Get('avatars/:username/:filename')
  async avartarImage(
    @Param('username') username: string,
    @Param('filename') filename: string,
    @Res() res: Response,
  ) {
    const filePath = path.join(
      process.cwd(),
      'public',
      'medias',
      'avatars',
      username,
      filename,
    );

    if (!fs.existsSync(filePath)) {
      throw new NotFoundException(
        SystemLang.getText('messages', 'file_not_found'),
      );
    }

    res.sendFile(filePath);
  }

  @Get('products/:title/:filename')
  async serveImage(
    @Param('title') title: string,
    @Param('filename') filename: string,
    @Res() res: Response,
  ) {
    const filePath = path.join(
      process.cwd(),
      'public',
      'medias',
      'products',
      title,
      filename,
    );

    if (!fs.existsSync(filePath)) {
      throw new NotFoundException(
        SystemLang.getText('messages', 'file_not_found'),
      );
    }

    res.sendFile(filePath);
  }
}
