<?php

namespace App\Http\Controllers\Auth\Admin;

use App\Http\Controllers\Controller;
use App\Services\Auth\AuthService;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Exception;

class Auth extends Controller
{
    protected $authService;

    public function __construct(AuthService $authService)
    {
        $this->authService = $authService;
    }

    /**
     * 管理员登录
     */
    public function login(Request $request)
    {
        try {
            $validated = $request->validate([
                'username' => 'required|string',
                'password' => 'required|string',
            ]);

            $result = $this->authService->login($validated);

            return response()->json([
                'code' => 200,
                'message' => '登录成功',
                'data' => $result,
            ]);
        } catch (ValidationException $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => $e->errors(),
            ], 422);
        } catch (Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '登录失败：' . $e->getMessage(),
                'data' => null,
            ], 500);
        }
    }

    /**
     * 管理员登出
     */
    public function logout(Request $request)
    {
        try {
            $this->authService->logout();

            return response()->json([
                'code' => 200,
                'message' => '登出成功',
                'data' => null,
            ]);
        } catch (Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '登出失败：' . $e->getMessage(),
                'data' => null,
            ], 500);
        }
    }

    /**
     * 刷新token
     */
    public function refresh(Request $request)
    {
        try {
            $result = $this->authService->refresh();

            return response()->json([
                'code' => 200,
                'message' => '刷新成功',
                'data' => $result,
            ]);
        } catch (Exception $e) {
            return response()->json([
                'code' => 401,
                'message' => 'Token无效或已过期',
                'data' => null,
            ], 401);
        }
    }

    /**
     * 获取当前用户信息
     */
    public function me(Request $request)
    {
        try {
            $result = $this->authService->me();

            return response()->json([
                'code' => 200,
                'message' => 'success',
                'data' => $result,
            ]);
        } catch (Exception $e) {
            return response()->json([
                'code' => 401,
                'message' => '未登录或token已过期',
                'data' => null,
            ], 401);
        }
    }

    /**
     * 找回密码
     */
    public function resetPassword(Request $request)
    {
        try {
            $validated = $request->validate([
                'username' => 'required|string',
                'password' => 'required|string|min:6|confirmed',
            ]);

            $this->authService->resetPassword($validated);

            return response()->json([
                'code' => 200,
                'message' => '密码重置成功',
                'data' => null,
            ]);
        } catch (ValidationException $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => $e->errors(),
            ], 422);
        } catch (Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '密码重置失败：' . $e->getMessage(),
                'data' => null,
            ], 500);
        }
    }

    /**
     * 修改密码
     */
    public function changePassword(Request $request)
    {
        try {
            $validated = $request->validate([
                'old_password' => 'required|string',
                'password' => 'required|string|min:6|confirmed',
            ]);

            $this->authService->changePassword($validated);

            return response()->json([
                'code' => 200,
                'message' => '密码修改成功',
                'data' => null,
            ]);
        } catch (ValidationException $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => $e->errors(),
            ], 422);
        } catch (Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '密码修改失败：' . $e->getMessage(),
                'data' => null,
            ], 500);
        }
    }
}
