<?php

namespace App\Services;

use App\Http\Resources\Order\OrderInfoResource;
use App\Http\Resources\Order\OrderReportResource;
use App\Http\Resources\Order\OrderSyncResource;
use App\Models\Order;

class OrderService {
    
    public function getOrderSync($orderId)
    {
        $order = Order::with([
            'customer',
            'items.variant.product.category',
            'items.variant.product.unit',
            'items.variant.inventory'
        ])
        ->where('uuid', $orderId)
        ->first();

        return new OrderSyncResource($order);
    }

    public function getOrders($customerId)
    {
        $tenantId = config('app.current_tenant_id');

        $orders = Order::select([
        'orders.uuid',
        'orders.order_number',
        'orders.total_amount', 
        'orders.order_status',
        'orders.delivery_type', 
        'orders.has_been_assessed',
        'orders.created_at'
        ])
        ->with(['items' => function ($q) {
            $q->select(
            'order_items.order_id',
            'order_items.product_image_url_snapshot'
            )
            ->take(2);
        }])
        ->withCount(['items as total_items'], 'total_items')
        ->orderByDesc('orders.created_at')
        ->where('orders.tenant_id', $tenantId)
        ->where('orders.customer_id', $customerId)
        ->where('orders.source', 'online')
        ->get();

        return OrderReportResource::collection($orders);
    }

    public function getOrderById($customerId, $orderId)
    {
        $tenantId = config('app.current_tenant_id');

        $orders = Order::select([
        'orders.uuid',
        'orders.order_number',
        'orders.created_at',
        'orders.order_status',
        'orders.is_cancellation',
        'orders.cancellation_accepted',
        'orders.approval_process', 
        'orders.delivery_type',
        'orders.delivery_preference',
        'orders.outlet_name_snapshot',
        'orders.outlet_address_snapshot', 
        'orders.customer_address_snapshot',
        'orders.payment_method',
        'orders.payment_proof_url',
        'orders.total_order',
        'orders.delivery_fee_type',
        'orders.delivery_fee',
        'orders.total_delivery_fee',
        'orders.total_amount'
        ])
        ->with(['items' => function ($q1) {
            $q1->select(
            'order_items.order_id' ,
            'order_items.product_variant_id',
            'order_items.product_image_url_snapshot', 
            'order_items.product_name_snapshot',
            'order_items.product_variant_name_snapshot', 
            'order_items.selling_price_snapshot',
            'order_items.quantity'
            )
            ->with(['variant' => function($q2) {
                $q2->select(
                'product_variants.uuid',
                'product_variants.product_id as product_id'
                );
            }]);
        }])
        ->where('orders.uuid', $orderId)
        ->where('orders.tenant_id', $tenantId)
        ->where('orders.customer_id', $customerId)
        ->limit(1)
        ->first();

        return new OrderInfoResource($orders);
    }

    public function cancelOrder($customerId, $orderId)
    {
        $tenantId = config('app.current_tenant_id');

        $order = Order::select([
        'orders.uuid',
        'orders.outlet_id',
        'orders.updated_at'
        ])
        ->where('uuid', $orderId)
        ->where('tenant_id', $tenantId)
        ->where('customer_id', $customerId)
        ->first();

        $order->update([
            'already_read' => false,
            'order_status' => 'canceled',
            'canceled_by' => 'customer',
        ]);

        return $order;
    }

    public function cancellationRequest($customerId, $orderId)
    {
        $tenantId = config('app.current_tenant_id');

        $order = Order::select([
        'orders.uuid',
        'orders.outlet_id',
        'orders.updated_at'
        ])
        ->where('uuid', $orderId)
        ->where('tenant_id', $tenantId)
        ->where('customer_id', $customerId)
        ->first();

        $order->update([
            'already_read' => false,
            'is_cancellation' => true,
            'approval_process' => 'waiting',
        ]);

        return $order;
    }
}