<?php

namespace Modules\CRMCore\App\Http\Resources;

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

class CustomerResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        // Get primary address information if loaded
        $primaryAddress = $this->relationLoaded('primaryAddress') ? $this->primaryAddress : null;

        return [
            'id' => $this->id,
            'customer_code' => $this->customer_code,
            'name' => $this->name,
            'company_name' => $this->company_name,
            'email' => $this->email,
            'phone' => $this->phone,
            'mobile' => $this->mobile,
            'website' => $this->website,
            'customer_type' => $this->customer_type,
            'customer_type_label' => $this->customer_type?->label(),
            'status' => $this->status,
            'status_label' => $this->status?->label(),
            'tax_id' => $this->tax_id,
            'vat_number' => $this->vat_number,
            'tax_exempt' => $this->tax_exempt,

            // Address information from primary address
            'billing_address' => $primaryAddress?->address_line_1,
            'billing_city' => $primaryAddress?->city,
            'billing_state' => $primaryAddress?->state,
            'billing_country' => $primaryAddress?->country,
            'billing_postal_code' => $primaryAddress?->postal_code,
            'full_address' => $primaryAddress?->full_address,

            // Customer financial fields
            'credit_limit' => $this->credit_limit,
            'credit_used' => $this->credit_used,
            'available_credit' => $this->available_credit,
            'payment_terms' => $this->payment_terms,
            'discount_percentage' => $this->discount_percentage,
            'preferred_payment_method' => $this->preferred_payment_method,
            'preferred_delivery_method' => $this->preferred_delivery_method,

            // Purchase history
            'lifetime_value' => $this->lifetime_value,
            'first_purchase_date' => $this->first_purchase_date,
            'last_purchase_date' => $this->last_purchase_date,
            'purchase_count' => $this->purchase_count,
            'average_order_value' => $this->average_order_value,

            // Status fields
            'is_active' => $this->is_active,
            'is_blacklisted' => $this->is_blacklisted,
            'blacklist_reason' => $this->blacklist_reason,
            'notes' => $this->notes,
            'tags' => $this->tags,

            // Relationships
            'customer_group' => $this->whenLoaded('customerGroup', function () {
                return $this->customerGroup ? [
                    'id' => $this->customerGroup->id,
                    'name' => $this->customerGroup->name,
                    'discount_percentage' => $this->customerGroup->discount_percentage,
                ] : null;
            }),
            'company' => $this->whenLoaded('company', function () {
                return $this->company ? [
                    'id' => $this->company->id,
                    'name' => $this->company->name,
                    'website' => $this->company->website,
                ] : null;
            }),
            'primary_contact' => $this->whenLoaded('primaryContact', function () {
                return $this->primaryContact ? [
                    'id' => $this->primaryContact->id,
                    'name' => $this->primaryContact->name,
                    'email' => $this->primaryContact->email,
                    'phone' => $this->primaryContact->phone,
                ] : null;
            }),
            'primary_address' => $this->whenLoaded('primaryAddress', function () {
                return $this->primaryAddress ? [
                    'id' => $this->primaryAddress->id,
                    'full_address' => $this->primaryAddress->full_address,
                    'city' => $this->primaryAddress->city,
                    'country' => $this->primaryAddress->country,
                ] : null;
            }),
            'created_by' => $this->whenLoaded('createdBy', function () {
                return $this->createdBy ? [
                    'id' => $this->createdBy->id,
                    'name' => $this->createdBy->name,
                ] : null;
            }),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
