<?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 SendAbsenteesSummary extends BaseCommand
{
    protected $group       = 'Attendance';
    protected $name        = 'attendance:absentees-summary';
    protected $description = 'Sends daily attendance summaries to parents.';

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

        // Fetch today’s absent records (replace with your logic)
        $absentRecords = $attendanceModel->getTodayAbsentees();

        foreach ($absentRecords 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 absent on {$record['date']}.",
                ['in_app', 'email']
            );

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

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