<?php

namespace App\Actions\Attribution\Equipment;

use App\Traits\CrudResponse;
use App\Models\Settings\Modele;
use Illuminate\Support\Facades\Auth;
use App\Models\Attribution\Equipment;
use App\Actions\Attribution\Attribution\SetAttribution;

class SetElectricPower
{
    // Call trait
    use CrudResponse;

    public function __construct(
        private readonly SetAttribution $attr,
    ) {
    }


    /**
     * @param array $data
     * 
     * @return string
     */
    public function electricpower(array $data) : string
    {   
        // Get model
        $modele = Modele::find($data['modele_id']);
        
        // Add data to the array
        $updateData = [
            'numberofsockets'     => trim(mb_strtolower($data['numberofsockets'])),
            'capacity' => trim(mb_strtolower($data['capacity'])),
            'brand_id'          => $modele->brand_id,
            'modele_id'         => (int)$data['modele_id'],
            'active'            => 1,
            'created_by'        => Auth::id(),
            'updated_by'        => !empty($data['rowID']) ? Auth::id() : null,
        ];

        // Set data
        $response = Equipment::updateOrCreate(['id' => $data['rowID']], $updateData);

        // Set Attr
        $this->attr->setAttr($response , $data['attr_date'] , $data['service_id']);

        // return
        return 'Equipement ajouté avec succès';
    }
}
