<?php

namespace App\Filament\Resources\OrderResource\Pages;

use App\Enums\OrderAttributes;
use App\Filament\Resources\OrderResource;
use App\Models\Order;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Resources\Pages\EditRecord;

class EditOrder extends EditRecord
{
    protected static string $resource = OrderResource::class;

    protected function mutateFormDataBeforeFill(array $data): array
    {
        $order = Order::findOrFail($data['id']);

        // Order Products
        foreach ($order->orderProducts as $key => $product) {
            $data['order_products'][$key] = [
                'sku'          => $product->sku,
                'product_name' => $product->product_name,
                'color'        => $product->color,
                'xs'           => $product->productSizes->where('size', 'xs')->first()->amount ?? null,
                's'            => $product->productSizes->where('size', 's')->first()->amount ?? null,
                'm'            => $product->productSizes->where('size', 'm')->first()->amount ?? null,
                'l'            => $product->productSizes->where('size', 'l')->first()->amount ?? null,
                'xl'           => $product->productSizes->where('size', 'xl')->first()->amount ?? null,
                '2xl'          => $product->productSizes->where('size', '2xl')->first()->amount ?? null,
                '3xl'          => $product->productSizes->where('size', '3xl')->first()->amount ?? null,
                'osfa'         => $product->productSizes->where('size', 'osfa')->first()->amount ?? null,
            ];
        }

        // Product Services
        foreach ($order->productServices as $key => $service) {
            $data['services'][$key] = [
                'service_type'           => $service->service_type ?? '',
                'placement'              => $service->placement ?? '',
                'amount'                 => $service->amount ?? '',
                'amount_price'           => $service->amount_price ?? '',
                'notes'                  => $service->notes ?? '',
                'serviceFileName'        => $service->serviceFile->name ?? '',
                'serviceFileWidth'       => $service->serviceFile->width ?? '',
                'serviceFileHeight'      => $service->serviceFile->height ?? '',
                'serviceFileCode'        => $service->serviceFile->code ?? '',
                'serviceFileSetupNumber' => $service->serviceFile->setup_number ?? '',
            ];
        }

        foreach (OrderAttributes::cases() as $case) {
            if ($data[$case->name]) {
                $data['order_attributes'][] = $case->value ?? null;
            }
        }

        return $data;
    }

    protected function mutateFormDataBeforeSave(array $data): array
    {
        foreach (OrderAttributes::cases() as $case) {
            $data[$case->name] = false;
        }

        $data['order_attributes'] = array_filter($data['order_attributes']);

        foreach ($data['order_attributes'] as $attribute) {
            $data[OrderAttributes::from($attribute)->name] = true;
        }

        unset($data['order_attributes']);

        return $data;
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('print')
                ->icon('heroicon-s-printer')
                ->url(fn (Order $record) => route('orders.pdf', $record))
                ->openUrlInNewTab(),
            Actions\DeleteAction::make()
                ->icon('heroicon-s-trash'),
        ];
    }
}
