<?php

namespace App\Services\AttendanceTracking;

use App\Models\ParentNotification;
use Illuminate\Support\Facades\Log;
use Throwable;

class AttendanceNotificationLogService
{
    public function __construct(
        protected ParentNotification $notificationModel
    ) {
    }

    public function notificationAlreadySent(
        int $studentId,
        string $code,
        string $ymd,
        string $channel = 'email',
        ?string $to = null
    ): bool {
        try {
            if (method_exists($this->notificationModel, 'hasSent')) {
                return (bool) $this->notificationModel->hasSent($studentId, $code, $ymd, $channel, $to);
            }

            $query = $this->notificationModel->query()
                ->where('student_id', $studentId)
                ->where('code', $code)
                ->where('incident_date', $ymd)
                ->where('channel', $channel);

            if (!empty($to)) {
                $query->where('to_address', $to);
            }

            return $query->exists();
        } catch (Throwable $e) {
            Log::debug('notificationAlreadySent(): ' . $e->getMessage());
            return false;
        }
    }

    public function logNotification(
        int $studentId,
        string $code,
        string $ymd,
        string $channel,
        ?string $to,
        string $subject,
        string $status,
        string $response = '',
        ?string $semester = null,
        ?string $schoolYear = null
    ): void {
        try {
            $existingQuery = $this->notificationModel->query()
                ->where('student_id', $studentId)
                ->where('code', $code)
                ->whereDate('incident_date', $ymd)
                ->where('channel', $channel);

            if (!empty($to)) {
                $existingQuery->where('to_address', $to);
            }

            $existing = $existingQuery->orderByDesc('id')->first();

            if ($existing && !empty($existing->id)) {
                $existing->update([
                    'subject'      => $subject,
                    'status'       => $status,
                    'response'     => $response,
                    'semester'     => $semester,
                    'school_year'  => $schoolYear,
                    'updated_at'   => now(),
                ]);
            } else {
                $this->notificationModel->query()->create([
                    'student_id'    => $studentId,
                    'code'          => $code,
                    'incident_date' => $ymd,
                    'channel'       => $channel,
                    'to_address'    => $to,
                    'subject'       => $subject,
                    'status'        => $status,
                    'response'      => $response,
                    'semester'      => $semester,
                    'school_year'   => $schoolYear,
                ]);
            }
        } catch (Throwable $e) {
            Log::debug('logNotification(): ' . $e->getMessage());
        }
    }
}
