/**
 * Lego 13 — Codebase Mutation: REST analyze endpoint stripped.
 *
 * POST /palm-oil/analyze has been replaced by the VisionGateway (Socket.io).
 * Angular must no longer upload images via multipart HTTP.
 * It must emit vision:stream with a raw Base64 string instead (Lego 11).
 *
 * Read-only REST endpoints (history, archive) are preserved — they serve
 * the Angular history tab and do not conflict with the Hoarding architecture.
 */

import { Controller, Get, Delete, Res, Param } from '@nestjs/common';
import { PalmOilService } from './palm-oil.service';
import { Response } from 'express';
import * as fs from 'fs';

@Controller('palm-oil')
export class PalmOilController {
  constructor(private readonly palmOilService: PalmOilService) { }

  @Get('history')
  async getHistory() {
    return this.palmOilService.getHistory();
  }

  @Delete('history/:archiveId')
  async deleteRecord(@Param('archiveId') archiveId: string) {
    return this.palmOilService.deleteRecord(archiveId);
  }

  @Delete('history')
  async clearAllHistory() {
    return this.palmOilService.clearAllHistory();
  }

  @Get('archive/:id')
  async getArchivedImage(@Param('id') id: string, @Res() res: Response) {
    const record = await this.palmOilService.getRecordByArchiveId(id);
    if (!record || !record.image_path) {
      return res.status(404).send('Image not found');
    }

    res.setHeader('Content-Type', 'image/jpeg');
    const fileStream = fs.createReadStream(record.image_path);
    fileStream.pipe(res);
  }
}
