<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TicketLog extends Model
{

    public function supportTicket()
    {
        return $this->belongsTo(SupportTicket::class);
    }

    public static function storeLog($action, $ticketId, $adminId = 0, $remark = null, $assignStaffId = 0, $ticket = null)
    {
        $ticketLog                    = new TicketLog();
        $ticketLog->support_ticket_id = $ticketId;
        $ticketLog->admin_id          = $adminId;
        $ticketLog->assign_staff_id   = $assignStaffId;
        $ticketLog->action            = $action;
        $ticketLog->remark            = $remark;
        $ticketLog->save();

        if ($ticket && $ticket->department->feedback_request) {
            self::sendFeedbackRequest($ticket);
        }
    }

    public static function sendFeedbackRequest($ticket)
    {
        $sendFromData = [
            'email_address' => $ticket->department->email ?? company('email'),
            'name'          => $ticket->department->name,
        ];

        notify($ticket->user, 'FEEDBACK_REQUEST', [
            'ticket_subject'  => $ticket->subject,
            'ticket_number'   => $ticket->ticket_number,
            'department_name' => $ticket->department->name,
        ], ['email'], sendFromData: $sendFromData);
    }
}
