<?php

namespace App\Http\Resources\Fees;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class FeeRefundResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $students = $this->resource['students'] ?? [];

        return [
            'refund_amount' => (float) ($this->resource['refund_amount'] ?? 0),
            'total_paid' => (float) ($this->resource['total_paid'] ?? 0),
            'refund_deadline' => $this->resource['refund_deadline'] ?? null,
            'school_end_date' => $this->resource['school_end_date'] ?? null,
            'weeks_of_study' => (float) ($this->resource['weeks_of_study'] ?? 0),
            'withdrawn_students' => (int) ($this->resource['withdrawn_students'] ?? 0),
            'students' => array_map(function (array $row): array {
                return [
                    'student_id' => $row['student_id'] ?? null,
                    'grade' => $row['grade'] ?? null,
                    'grade_level' => $row['grade_level'] ?? null,
                    'fee_category' => $row['fee_category'] ?? null,
                    'tuition_fee' => (float) ($row['tuition_fee'] ?? 0),
                    'withdrawal_date' => $row['withdrawal_date'] ?? null,
                    'weeks_remaining' => (int) ($row['weeks_remaining'] ?? 0),
                    'refund_amount' => (float) ($row['refund_amount'] ?? 0),
                    'eligible' => (bool) ($row['eligible'] ?? false),
                    'reason' => $row['reason'] ?? null,
                ];
            }, $students),
        ];
    }
}
