<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class VoucherResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $filteredRedemptions = $this->redemptions->filter(function ($redemption) {
            return $redemption->transaction && $redemption->transaction->owner;
        });
        return [
            'id' => $this->id,
            'name' => $this->name,
            'code' => $this->code,
            'type' => $this->type,
            'value' => (float) $this->value,
            'start_date' => $this->start_date,
            'end_date' => $this->end_date,
            'is_redeemed' => $filteredRedemptions->count() > 0
        ];
    }
}
