<?php

namespace App\Http\Resources;

use App\Helpers\Backoffice\MediaHelper;
use App\Http\Resources\Specialisations\SpecialisationResource;
use App\Models\Subscription;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */

    public function toArray($request)
    {
        $member = $this->accountMember;
        $account = $member ? $member->account : null;
        $profile = $account ? $account->profile : null;
        $activeSubscription = $account ? $account->activeSubscriptions->first() : null;

        if (!$activeSubscription &&  $account &&$account->owner_id !== $this->id) {
            $account = null;
            $profile = null;
        }

        $out = [
            'salutation' => $this->salutation,
            'firstName' => $this->first_name,
            'lastName' => $this->last_name,
            'email' => $this->email,
            'status' => $this->status,
            'newsletter' => $this->newsletter,
            'account' => $account ? [
                'type' => $account->type,
                'is_admin' => $member->is_admin,
                'is_owner' => $this->id === $account->owner->id,
                'taxId' => $account->tax_id,
                'address' => $account->address ? [
                    'name' => $account->address->name,
                    'address' => $account->address->address,
                    'floor' => $account->address->floor,
                    'postalCode' => $account->address->postal_code,
                    'location' => $account->address->location,
                    'countryId' => $account->address->country_id,
                    'phonePrefix' => $account->address->phone_prefix,
                    'phone' => $account->address->phone,
                ] : null,
                'billingAddress' => $account->billingAddress ? [
                    'name' => $account->billingAddress->name,
                    'address' => $account->billingAddress->address,
                    'floor' => $account->billingAddress->floor,
                    'postalCode' => $account->billingAddress->postal_code,
                    'location' => $account->billingAddress->location,
                    'countryId' => $account->billingAddress->country_id,
                    'phonePrefix' => $account->billingAddress->phone_prefix,
                    'phone' => $account->billingAddress->phone,
                ] : null,
                'subscription' => $activeSubscription ? [
                    'id' => $activeSubscription->subscriptionPlan->id,
                    'status' => $activeSubscription->status,
                    'plan' => $activeSubscription->subscriptionPlan->name,
                    'is_trial' => $activeSubscription->is_trial,
                    'trial_end_date' => $activeSubscription->trial_end_date,
                    'seats' => $activeSubscription->seats,
                    'features' => $activeSubscription->subscriptionPlan->features->pluck('name'),
                    'subscriptionReward' => $activeSubscription->subscriptionReward ? [
                        'id' => $activeSubscription->subscriptionReward->id,
                        'title' => $activeSubscription->subscriptionReward->title,
                    ] : null,
                ] : null,
            ] : null,
            'companyProfile' => $profile ? [
                'id' => $profile->id,
                'name' => $profile->name,
                'employeeCount' => $profile->employee_count,
                'email' => $profile->email,
                'phone' => $profile->phone,
                'phonePrefix' => $profile->phone_prefix,
                'websiteUrl' => $profile->website_url,
                'companyImageThumb' => MediaHelper::getCompanyImageUrl($profile->company_image, '200x'),
                'companyImage' => MediaHelper::getCompanyImageUrl($profile->company_image, '600x'),
                'coverImage' => MediaHelper::getCompanyImageUrl($profile->cover_image, '200x'),
                'coverImageName' => $profile->cover_image_original,
                'coverImageSize' => $profile->cover_image_size,
                'description' => $profile->description,
                'subtitle' => $profile->subtitle,
                'location' => $profile->location,
                'instagramUrl' => $profile->instagram_url,
                'linkedinUrl' => $profile->linkedin_url,
                'facebookUrl' => $profile->facebook_url,
                'isActive' => $profile->is_active,
                'companySpecialisations' => $profile->companySpecialisations->pluck('id'),
            ] : null,
            'image' => MediaHelper::getProfileImageUrl($this->profile_image, '200x'),
            'user_profile_role_id' => $this->user_profile_role_id,
            'userProfileRole' => $this->profileRole->title ?? null,
            'user_profile_specialisation_id' => $this->user_profile_specialisation_id,
            'job_profile' => $this->job_profile,
            'registrationStep' => $this->registration_step,
            'registrationPlanOptionId' => $this->registration_plan_option_id,
            'isBasicUser' => $this->isBasicUser(),
        ];

        return $out;
    }
}
