<?php

namespace App\Http\Controllers\Emails;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
use App\Models\SalaryStructure;
use App\Mail\SalaryStructureMail;

/**
 * Email controller for table. Sends table-specific emails.
 */
class SalaryStructureEmailController extends Controller
{
    /**
     * Send email for a specific record.
     * Expects the model to have an 'email' column for the recipient.
     */
    public function send(Request $request, $id)
    {
        $row = SalaryStructure::findOrFail($id);

        if (empty($row->email)) {
            return response()->json(['error' => 'This record has no email address.'], 422);
        }

        $getSettings = View::shared('getSettings');
        $appUrl = env('APP_URL');
        $siteName = $getSettings['site_name'] ?? config('app.name');

        Mail::to($row->email)->queue(new SalaryStructureMail(
            $row,
            $request->input('subject', 'Notification - ' . $siteName),
            $request->input('message', ''),
            $siteName,
            $appUrl
        ));

        return response()->json(['success' => 'Email has been queued successfully.']);
    }
}
