<?php

namespace App\Http\Controllers\Material;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Material\AssignMaterialRequest;
use App\Http\Requests\Material\IndexMaterialRequest;
use App\Http\Requests\Material\ReturnMaterialRequest;
use App\Http\Requests\Material\TransferMaterialRequest;
use App\Http\Resources\Material\MaterialResource;
use App\Services\Material\MaterialService;

class MaterialController extends ApiController
{
    protected $service;

    public function __construct(MaterialService $service)
    {
        $this->service = $service;
    }

    public function index(IndexMaterialRequest $request)
    {
        $data = $this->service->list($request->validated());

        return $this->successPaginated(
            $data,
            MaterialResource::class,
            'List material user berhasil diambil'
        );
    }

    public function assign(AssignMaterialRequest $request)
    {
        $result = $this->service->assign($request->validated());

        return $this->created(
            new MaterialResource($result),
            'Material berhasil di-assign'
        );
    }

    public function transfer(TransferMaterialRequest $request)
    {
        $result = $this->service->transfer($request->validated());

        return $this->updated(
            new MaterialResource($result),
            'Material berhasil di-transfer'
        );
    }

    public function return(ReturnMaterialRequest $request)
    {
        $result = $this->service->return($request->validated());

        return $this->updated(
            new MaterialResource($result),
            'Material berhasil direturn'
        );
    }

    public function listByUser($user_id)
    {
        $data = $this->service->listByUser($user_id);

        return $this->success(
            MaterialResource::collection($data),
            'List material user berhasil diambil'
        );
    }

    public function history($barcode_id)
    {
        $data = $this->service->history($barcode_id);

        return $this->success(
            $data,
            'History material berhasil diambil'
        );
    }
}
