<?php

namespace App\Http\Resources\Users;

use App\Models\Subscription;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserPublicResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        // $subscription = Subscription::where('user_id', $this->id)
        //     ->where('expires_at', '>', now())
        //     ->latest('expires_at')
        //     ->first();
        $subscription = Subscription::where('user_id', $this->id)
            // ->where('is_active', true)
            ->whereNotNull('expires_at')
            ->where('expires_at', '>', now())
            ->latest('expires_at')
            ->first();

        $isSubscribed = false;
        $remaining = [
            'days' => 0,
            'hours' => 0,
            'minutes' => 0,
            'seconds' => 0,
        ];

        if ($subscription && $subscription->expires_at >= now()) {
            $isSubscribed = true;

            $diff = now()->diff($subscription->expires_at);

            $remaining = [
                'days' => $diff->d,
                'hours' => $diff->h,
                'minutes' => $diff->i,
                'seconds' => $diff->s,
            ];
        }

        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'social_id' => $this->social_id,
            'social_type' => $this->social_type,
            'photo' => $this->photo,

            'is_subscribed' => $isSubscribed,
            'remaining_subscription' => $remaining,



        ];
    }
}
