<?php

namespace App\Http\Controllers\SM;

use App\Http\Controllers\Controller;
use App\Http\Requests\SM\UserRequest;
use App\Models\User;
use App\Services\SM\UserService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
use Illuminate\Support\Facades\DB;

class UserController extends Controller implements HasMiddleware
{
    /**
     * Display a listing of the resource.
     */
    public static function middleware(): array
    {
        return [
            new Middleware('permission:USER.VIEW',only:['index'])
        ];
    }
    public function __construct(private UserService $userService)
    {
    }

    public function index()
    {
        $data = $this->userService->dataTable();
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(UserRequest $request)
    {
        try {
            DB::beginTransaction();
            $user = $this->userService->create($request->all());
            DB::commit();
            return response()->json(['user' => $user]);
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json(['message' => $e->getMessage()], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        try {
            return response()->json(['user' => $this->userService->detail($id)]);
        } catch (Exception $e) {
            return response()->json(['message' => $e->getMessage()], 403);
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UserRequest $request, string $id)
    {
        try {
            DB::beginTransaction();
            $user = $this->userService->update($id, $request->all());
            DB::commit();
            return response()->json(['success' => true, 'data' => $user, 'message' => "User updated"]);
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json(['success' => false, 'message' => $e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }

    public function remove(Request $request)
    {
        try {
            DB::beginTransaction();
            $this->userService->remove($request->all());
            DB::commit();
            return response()->json(['success' => true, 'data' => [], 'message' => "User deleted"]);
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json(['success' => false, 'message' => $e->getMessage()]);
        }
    }
}
