<?php

namespace App\Service\Shop;

use App\Models\RequisitionDetailes;
use App\Models\RequisitionMaster;
use App\Services\NotificationService;
use Illuminate\Support\Facades\Auth;

class RequisitionService
{
    public static function DataStore(array $data)
    {
        $collect = collect($data);
   
        // Create purchase Master
        $master_id = RequisitionMaster::create([
            'invoice_no' => $collect['invoice_no'],
            'requisition_date' => $collect['requisition_date'],
            'remark' => isset($collect['remark']) ? $collect['remark'] : null,
            'shop_id' => Auth::user()->shop_id,
            'client_id' => isset($collect['client_id']) ? $collect['client_id'] : null,
            'location_id' => isset($collect['location_id']) ? $collect['location_id'] : null,
            'branch_id' => isset($collect['branch_id']) ? $collect['branch_id'] : null,
            'department_id' => isset($collect['department_id']) ? $collect['department_id'] : null,
            'contact_person_id' => isset($collect['contact_person_id']) ? $collect['contact_person_id'] : null,
            'created_by' => Auth::user()->id,
        ]);

        // Create Purchase Details
        foreach ($collect['list'] as $key => $value) {
            RequisitionDetailes::create([
                'requisition_masters_id' => $master_id->id,
                'product_id' => intval($value['product_id']),
                'attribute_id' => intval($value['attribute_id']),
                'quantity' => $value['quantity'],
                'price' => $value['price'],
                'total' => $value['total'],
                'note' => $value['note'] ?? null,

            ]);
        }

        // Send notification for new requisition
        // $requisition = RequisitionMaster::with('user.shop', 'shop')->find($master_id->id);
        // $redirectConfig = (object)[
        //     "route" => 'product-shifting',
        //     "warehouseId" => $collect['location_id'],
        // ];

        // $requisitionNotificationData = [
        //     'title' => 'New Requisition Created',
        //     'message' => "Requisition #{$requisition->invoice_no} from {$requisition->shop->name} has been created by {$requisition->user->name}",
        //     'requisition_id' => $requisition->id,
        //     'requisition_invoice' => $requisition->invoice_no,
        //     'created_by' => $requisition->user->name,
        //     'shop_name' => $requisition->user->shop->name ?? 'Unknown Shop'
        // ];

        // NotificationService::sendRequisitionCreatedNotification($requisitionNotificationData, $redirectConfig);

        return $master_id;
    }

    public static function DataUpdate(array $data, $id)
    {

        $collect = collect($data);
        // Update purchase Master
        $master_id = RequisitionMaster::where('id', $id)->update([
            'requisition_date' => $collect['requisition_date'],
            'remark' => $collect['remark'],
            'shop_id' => Auth::user()->shop_id,
            'updated_by' => Auth::user()->id,
            'client_id' => isset($collect['client_id']) ? $collect['client_id'] : null,
            'location_id' => isset($collect['location_id']) ? $collect['location_id'] : null,
            'branch_id' => isset($collect['branch_id']) ? $collect['branch_id'] : null,
            'department_id' => isset($collect['department_id']) ? $collect['department_id'] : null,
            'contact_person_id' => isset($collect['contact_person_id']) ? $collect['contact_person_id'] : null,
        ]);
        RequisitionDetailes::where('requisition_masters_id', $id)->delete();
        // Update Purchase Details
        foreach ($collect['list'] as $key => $value) {
            RequisitionDetailes::create([
                'requisition_masters_id' => $id,
                'product_id' => intval($value['product_id']),
                'attribute_id' => intval($value['attribute_id']),
                'quantity' => $value['quantity'],
                'price' => $value['price'],
                'total' => $value['total'],
                'note' => $value['note'] ?? null,

            ]);
        }
    }
}
