<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Helpers\Response;
use App\Services\NoteService;

class NoteController extends BaseController
{
    private NoteService $noteService;

    public function __construct($request)
    {
        parent::__construct($request);
        $this->noteService = new NoteService();
    }

    public function index(): void
    {
        $leadId = (int) $this->getRouteParam('id');
        $notes = $this->noteService->getNotesByLead($leadId);
        Response::success($notes, 'Notas consultadas correctamente');
    }

    public function storeText(): void
    {
        $leadId = (int) $this->getRouteParam('id');
        $data = $this->request->body();

        if (empty($data['content'] ?? null)) {
            Response::error('El contenido de la nota es obligatorio', 422);
        }

        try {
            $note = $this->noteService->createTextNote($leadId, $data['content'], $this->getAuthUserId());
            Response::created($note, 'Nota registrada correctamente');
        } catch (\RuntimeException $e) {
            Response::error($e->getMessage(), $e->getCode() ?: 400);
        }
    }

    public function storeVoice(): void
    {
        $leadId = (int) $this->getRouteParam('id');
        $file = $this->request->uploadedFile('audio');

        try {
            $note = $this->noteService->createVoiceNote($leadId, $file ?? [], $this->getAuthUserId());
            Response::created($note, 'Nota de voz registrada correctamente');
        } catch (\RuntimeException $e) {
            Response::error($e->getMessage(), $e->getCode() ?: 400);
        }
    }

    public function update(): void
    {
        $noteId = (int) $this->getRouteParam('noteId');
        $data = $this->request->body();

        if (empty($data['content'] ?? null)) {
            Response::error('El contenido de la nota es obligatorio', 422);
        }

        try {
            $note = $this->noteService->updateNote($noteId, $data['content']);
            Response::success($note, 'Nota actualizada correctamente');
        } catch (\RuntimeException $e) {
            Response::error($e->getMessage(), $e->getCode() ?: 400);
        }
    }

    public function serveAudio(): void
    {
        $noteId = (int) $this->getRouteParam('noteId');

        try {
            $filePath = $this->noteService->getAudioFilePath($noteId);
            $mime = mime_content_type($filePath) ?: 'audio/webm';
            $size = filesize($filePath);
            header('Content-Type: ' . $mime);
            header('Content-Length: ' . $size);
            header('Accept-Ranges: bytes');
            readfile($filePath);
            exit;
        } catch (\RuntimeException $e) {
            Response::error($e->getMessage(), $e->getCode() ?: 400);
        }
    }

    public function destroy(): void
    {
        $noteId = (int) $this->getRouteParam('noteId');

        try {
            $this->noteService->deleteNote($noteId);
            Response::success(null, 'Nota eliminada correctamente');
        } catch (\RuntimeException $e) {
            Response::error($e->getMessage(), $e->getCode() ?: 400);
        }
    }
}
