<?php

namespace Modules\Inventory\Transformers\Search;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Core\Enums\SearchTypeEnum;

class ProductSearchResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     */
    public function toArray(Request $request): array
    {
        $type = $request->enum('type', SearchTypeEnum::class);
        $isPurchase = $type === SearchTypeEnum::PURCHASE;
        $isBilling = $type === SearchTypeEnum::BILLING;
        $isAssetPurchase = $type === SearchTypeEnum::ASSET_PURCHASE;
        $defaultExists = false;

        if ($isPurchase || $isAssetPurchase) {
            $defaultExists = $this->purchaseUnitConversions->contains(
                'is_default_purchase', true
            );
        }

        if ($isBilling) {
            $defaultExists = $this->saleUnitConversions->contains(
                'is_default_sale', true
            );
        }

        /*
        |--------------------------------------------------------------------------
        | Base Unit Always Included
        |--------------------------------------------------------------------------
        */

        $units = [[
            'unit_id' => $this->baseUnit->id,
            'unit_name' => $this->baseUnit->name,
            'is_base_unit' => true,
            'from_qty' => 1,
            'to_qty' => 1,
            'selected_default' => !$defaultExists,
        ]];

        /*
        |--------------------------------------------------------------------------
        | Purchase Units
        |--------------------------------------------------------------------------
        */

        if ($isPurchase || $isAssetPurchase) {
            foreach ($this->purchaseUnitConversions as $conversion) {
                $units[] = [
                    'unit_id' => $conversion->fromUnit->id,
                    'unit_name' => $conversion->fromUnit->name,
                    'is_base_unit' => false,
                    'from_qty' => $conversion->from_qty,
                    'to_qty' => $conversion->to_qty,
                    'selected_default' => $conversion->is_default_purchase,
                ];
            }
        }

        /*
        |--------------------------------------------------------------------------
        | BILLING Units
        |--------------------------------------------------------------------------
        */

        if ($isBilling) {
            foreach ($this->saleUnitConversions as $conversion) {
                $units[] = [
                    'unit_id' => $conversion->fromUnit->id,
                    'unit_name' => $conversion->fromUnit->name,
                    'is_base_unit' => false,
                    'from_qty' => $conversion->from_qty,
                    'to_qty' => $conversion->to_qty,
                    'selected_default' => $conversion->is_default_sale,
                ];
            }
        }

        return [
            'id' => $this->id,
            'name' => $this->name,
            'sku' => $this->sku,
            'category_id' => $this->category_id,
            'category_type' => $this->category?->category_type,
            'product_type' => $this->product_type,
            'base_unit' => [
                'id' => $this->baseUnit->id,
                'name' => $this->baseUnit->name,
            ],
            'has_stock' => $this->has_stock,
            'has_batch' => $this->has_batch,
            'has_expiry' => $this->has_expiry,
            'has_unit_conversion' => $this->has_unit_conversion,
            'vat_price_type'      => $this->vat_price_type,
            'vat_type' => $this->vat_type,
            'vat_rate' => $this->vat_rate,
            'default_selling_price' => $this->default_selling_price,
            'selling_price' => $this->selling_price,
            'available_stock' => $this->available_stock,
            'units' => $units,
        ];
    }
}
