<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Models\UserType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserTypeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $types = UserType::with('category')->orderBy('name', 'asc')->get();
        return response()->json([
            'success' => true,
            'data' => $types
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'code' => 'required|string|unique:user_types,code|max:100',
            'description' => 'nullable|string',
            'user_category_id' => 'required|exists:user_categories,id',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'message' => 'Validation failed',
                'errors' => $validator->errors()
            ], 422);
        }

        $type = UserType::create($request->only(['name', 'code', 'description', 'user_category_id']));
        $type->load('category');
        
        return response()->json([
            'success' => true,
            'message' => 'User type created successfully',
            'data' => $type
        ], 201);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $type = UserType::with('category')->findOrFail($id);
        return response()->json([
            'success' => true,
            'data' => $type
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $type = UserType::findOrFail($id);

        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'user_category_id' => 'required|exists:user_categories,id',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'message' => 'Validation failed',
                'errors' => $validator->errors()
            ], 422);
        }

        $type->update($request->only(['name', 'description', 'user_category_id']));
        $type->load('category');
        
        return response()->json([
            'success' => true,
            'message' => 'User type updated successfully',
            'data' => $type
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $type = UserType::findOrFail($id);
        $type->delete();
        
        return response()->json([
            'success' => true,
            'message' => 'User type deleted successfully'
        ]);
    }
}
