<?php

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

use App\Http\Controllers\Controller;
use App\Services\CoachAuthService;
use App\Traits\ApiResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CoachAuthController extends Controller
{
    use ApiResponseTrait;

    public function __construct(
        protected CoachAuthService $authService
    ) {}

    /**
     * POST /auth/coach/login
     */
    public function login(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'email'       => ['required', 'email'],
            'password'    => ['required', 'string'],
            'device_name' => ['nullable', 'string'],
        ]);

        $result = $this->authService->login(
            $validated['email'],
            $validated['password'],
            $validated['device_name'] ?? 'api-token'
        );

        return $this->ApiResponse($result, __('Login successful'), 200);
    }

    /**
     * POST /coach/logout
     */
    public function logout(Request $request): JsonResponse
    {
        $request->user()->currentAccessToken()->delete();

        return $this->ApiResponse([], __('Logged out successfully'), 200);
    }
}
