<?php

namespace App\Domains\MailTest\Controllers;

use App\Domains\MailTest\Requests\SendTestMailRequest;
use App\Domains\MailTest\Services\MailTestService;
use App\Domains\Tenant\Models\Tenant;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class MailTestController extends Controller
{
    public function __construct(
        protected MailTestService $mailTestService,
    ) {}

    public function __invoke(SendTestMailRequest $request, string $tenantCode): JsonResponse
    {
        $tenant = Tenant::query()
            ->where('codigo', $tenantCode)
            ->firstOrFail();

        return response()->json(
            $this->mailTestService->send(
                $tenant,
                $request->validated('to'),
                $request->validated('subject'),
                $request->validated('message'),
            )
        );
    }
}
