<?php

namespace App\Http\Resources\Api\V1;

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

class UserResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'                => $this->id,
            'name'              => $this->name,
            'email'             => $this->email,
            'email_verified_at' => $this->email_verified_at,
            'branch_id'         => $this->branch_id,
            'tenant_id'         => $this->tenant_id,
            'branch' => $this->whenLoaded('branch', function () {
                return [
                    'id'   => $this->branch->id,
                    'name' => $this->branch->name,
                    'code' => $this->branch->code,
                ];
            }),
            'tenant' => $this->whenLoaded('tenant', function () {
                return [
                    'id'                  => $this->tenant->id,
                    'name'                => $this->tenant->name,
                    'slug'                => $this->tenant->slug,
                    'business_type'       => $this->tenant->business_type,
                    'business_type_label' => $this->tenant->getBusinessTypeLabel(),
                    'enabled_modules'     => $this->tenant->enabled_modules ?? [],
                    'settings'            => $this->tenant->settings ?? [],
                    'subscription_plan'   => $this->tenant->subscription_plan,
                ];
            }),
            'roles' => $this->whenLoaded('roles', function () {
                return $this->roles->pluck('name');
            }),
            'permissions' => $this->whenLoaded('permissions', function () {
                return $this->getAllPermissions()->pluck('name');
            }),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
