<?php
declare(strict_types=1);

namespace App\Application\Shopify;

use App\Framework\Api\ApiException;
use App\Framework\Attributes\Route;
use App\Framework\Http\Method;
use App\Framework\Http\Status;
use App\Framework\Router\Result\JsonResult;
use App\Infrastructure\Api\ShopifyClient;
use Archive\Config\ApiConfig;

final class OrderController
{
    private ShopifyClient $client;

    public function __construct()
    {
        $this->client = new ShopifyClient(
            ApiConfig::SHOPIFY_SHOP_DOMAIN->value,
            ApiConfig::SHOPIFY_ACCESS_TOKEN->value,
            ApiConfig::SHOPIFY_API_VERSION->value
        );
    }

    /**
     * Ruft alle Bestellungen ab
     */
    #[Route(path: '/api/shopify/orders', method: Method::GET)]
    public function listOrders(): JsonResult
    {
        try {
            $options = [
                'limit' => 50,
                'status' => 'any',
                'fields' => 'id,order_number,customer,total_price,created_at,financial_status'
            ];

            $orders = $this->client->getOrders($options);

            return new JsonResult([
                'success' => true,
                'data' => $orders
            ]);
        } catch (ApiException $e) {
            $result = new JsonResult([
                'success' => false,
                'error' => $e->getMessage()
            ]);
            $result->status = Status::from($e->getCode() ?: 500);
            return $result;
        }
    }

    /**
     * Ruft eine einzelne Bestellung ab
     */
    #[Route(path: '/api/shopify/orders/{id}', method: Method::GET)]
    public function getOrder(int $id): JsonResult
    {
        try {
            $order = $this->client->getOrder($id);

            return new JsonResult([
                'success' => true,
                'data' => $order
            ]);
        } catch (ApiException $e) {
            $result = new JsonResult([
                'success' => false,
                'error' => $e->getMessage()
            ]);
            $result->status = Status::from($e->getCode() ?: 500);
            return $result;
        }
    }

    /**
     * Erstellt eine neue Bestellung
     */
    #[Route(path: '/api/shopify/orders', method: Method::POST)]
    public function createOrder(OrderRequest $request): JsonResult
    {
        try {
            $orderData = [
                'line_items' => $request->lineItems,
                'customer' => $request->customer ?? null,
                'shipping_address' => $request->shippingAddress ?? null,
                'billing_address' => $request->billingAddress ?? null,
                'financial_status' => $request->financialStatus ?? 'pending',
                'fulfillment_status' => $request->fulfillmentStatus ?? null,
                'tags' => $request->tags ?? '',
                'note' => $request->note ?? null,
                'email' => $request->email ?? null
            ];

            // Entferne leere Felder
            $orderData = array_filter($orderData, fn($value) => $value !== null);

            $order = $this->client->createOrder($orderData);

            $result = new JsonResult([
                'success' => true,
                'data' => $order
            ]);
            $result->status = Status::CREATED;
            return $result;
        } catch (ApiException $e) {
            $result = new JsonResult([
                'success' => false,
                'error' => $e->getMessage()
            ]);
            $result->status = Status::from($e->getCode() ?: 500);
            return $result;
        }
    }
}
