<?php

namespace App\Modules\HR\Assets\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Assets\DTOs\AssetWriteData;
use App\Modules\HR\Assets\Models\Asset;
use App\Modules\HR\Assets\Repositories\AssetRepository;
use App\Modules\HR\Assets\Requests\AssetActionRequest;
use App\Modules\HR\Assets\Requests\AssetRequest;
use App\Modules\HR\Assets\Services\AssetService;
use App\Modules\HR\Employees\Repositories\EmployeeRepository;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class AssetController extends Controller
{
    public function __construct(
        private readonly AssetRepository $assets,
        private readonly AssetService $service,
        private readonly EmployeeRepository $employees,
    ) {}

    public function index(): View
    {
        $this->authorize('viewAny', Asset::class);

        return view('pages.hr.assets.index', ['assets' => $this->assets->paginate($this->tenantId())]);
    }

    public function create(): View
    {
        $this->authorize('create', Asset::class);

        return view('pages.hr.assets.create');
    }

    public function store(AssetRequest $request): RedirectResponse
    {
        $asset = $this->service->create($this->tenantId(), AssetWriteData::fromValidated($request->validated()));

        return redirect()->route('hr.assets.show', $asset)->with('status', __('assets.flash.created'));
    }

    public function show(Asset $asset): View
    {
        $this->authorize('view', $asset);

        return view('pages.hr.assets.show', [
            'asset' => $this->assets->details($asset),
            'employees' => $this->employees->listForSelect($this->tenantId()),
        ]);
    }

    public function edit(Asset $asset): View
    {
        $this->authorize('update', $asset);

        return view('pages.hr.assets.edit', compact('asset'));
    }

    public function update(AssetRequest $request, Asset $asset): RedirectResponse
    {
        $this->service->update($asset, AssetWriteData::fromValidated($request->validated()));

        return redirect()->route('hr.assets.show', $asset)->with('status', __('assets.flash.updated'));
    }

    public function assign(AssetActionRequest $request, Asset $asset): RedirectResponse
    {
        $this->service->assign($asset, (int) $request->validated('employee_id'), $request->validated('notes'));

        return $this->back($asset, 'assigned');
    }

    public function transfer(AssetActionRequest $request, Asset $asset): RedirectResponse
    {
        $this->service->transfer($asset, (int) $request->validated('employee_id'), $request->validated('notes'));

        return $this->back($asset, 'transferred');
    }

    public function return(AssetActionRequest $request, Asset $asset): RedirectResponse
    {
        $this->service->return($asset, $request->validated('notes'));

        return $this->back($asset, 'returned');
    }

    public function damage(AssetActionRequest $request, Asset $asset): RedirectResponse
    {
        $this->service->damage($asset, $request->validated('notes'));

        return $this->back($asset, 'damaged');
    }

    private function back(Asset $asset, string $message): RedirectResponse
    {
        return redirect()->route('hr.assets.show', $asset)->with('status', __('assets.flash.'.$message));
    }

    private function tenantId(): int
    {
        return (int) auth()->user()->tenant_id;
    }
}
