<?php

namespace App\Modules\SuperAdmin\Resources;

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

class AdminCompanyResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'slug' => $this->slug,
            'email' => $this->email,
            'phone' => $this->phone,
            'currency' => $this->currency,
            'timezone' => $this->timezone,
            'status' => $this->status,
            'users_count' => $this->users_count,
            'subscription' => $this->whenLoaded('activeSubscription', function (): ?array {
                if (! $this->activeSubscription) {
                    return null;
                }

                return [
                    'id' => $this->activeSubscription->id,
                    'status' => $this->activeSubscription->status,
                    'billing_cycle' => $this->activeSubscription->billing_cycle,
                    'trial_ends_at' => $this->activeSubscription->trial_ends_at,
                    'renews_at' => $this->activeSubscription->renews_at,
                    'plan' => [
                        'id' => $this->activeSubscription->plan->id,
                        'name' => $this->activeSubscription->plan->name,
                        'slug' => $this->activeSubscription->plan->slug,
                    ],
                ];
            }),
            'created_at' => $this->created_at,
        ];
    }
}
