<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

/** @mixin \Laravel\Cashier\Subscription */
class SubscriptionResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->type,
            'stripe_status' => $this->stripe_status,
            'plan' => $this->when($this->plan_name !== null, fn () => [
                'name' => $this->plan_name,
                'price' => $this->plan_price,
                'prices' => $this->plan?->prices?->mapWithKeys(fn ($p) => [
                    $p->billing_cycle => $p->price,
                ]) ?? [],
            ]),
            'billing_cycle' => $this->plan_billing_cycle ?? null,
            'current_period_end' => $this->current_period_end?->toIso8601String(),
            'ends_at' => $this->ends_at?->toIso8601String(),
            'created_at' => $this->created_at?->toIso8601String(),
        ];
    }
}
