<?php

namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use App\Models\AttendanceDataModel;
use App\Models\UserModel;
use App\Services\NotificationService;

class SendLatesSummary extends BaseCommand
{
    protected $group       = 'Attendance';
    protected $name        = 'attendance:lates-summary';
    protected $description = 'Sends daily attendance summaries to parents.';

    public function run(array $params)
    {
        $attendanceModel = new AttendanceDataModel();
        $userModel = new UserModel();

        // Fetch today’s late records (replace with your logic)
        $lateRecords = $attendanceModel->getTodayLates();

        foreach ($lateRecords as $record) {
            // Get parent user ID
            $parent = $userModel->find($record['parent_id']);
            if (!$parent) continue;

            NotificationService::toUser(
                $parent['id'],
                'Daily Attendance Update',
                "{$record['student_name']} was marked late on {$record['date']}.",
                ['in_app', 'email']
            );

            CLI::write("Sent summary to parent: {$parent['email']}", 'yellow');
        }

        CLI::write("Attendance summary completed.", 'green');
    }
}
