<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\LedgerEntryResource;
use App\Http\Resources\TherapistResource;
use App\Models\User;
use App\Services\LedgerService;
use App\Services\WageService;
use Illuminate\Http\Request;

class TherapistController extends Controller
{
    protected $ledgerService;
    protected $wageService;

    public function __construct(LedgerService $ledgerService, WageService $wageService)
    {
        $this->ledgerService = $ledgerService;
        $this->wageService = $wageService;
    }

    public function index(Request $request)
    {
        $query = User::whereIn('role', ['therapist', 'admin', 'manager']);

        if ($request->has('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                  ->orWhere('email', 'like', "%{$search}%")
                  ->orWhere('specialization', 'like', "%{$search}%");
            });
        }

        $therapists = $query->orderBy('name')->paginate($request->per_page ?? 20);

        return TherapistResource::collection($therapists);
    }

    public function show(User $user)
    {
        return new TherapistResource($user);
    }

    public function update(Request $request, User $user)
    {
        $data = $request->validate([
            'name' => 'sometimes|string|max:255',
            'specialization' => 'nullable|string|max:255',
            'hourly_rate' => 'nullable|numeric|min:0',
            'session_rate' => 'nullable|numeric|min:0',
            'target_sessions_per_day' => 'nullable|integer|min:0',
            'target_sessions_per_month' => 'nullable|integer|min:0',
            'working_schedule' => 'nullable|array',
        ]);

        $user->update($data);

        return new TherapistResource($user);
    }

    public function performance(Request $request, User $user)
    {
        $request->validate([
            'period_start' => 'required|date',
            'period_end' => 'required|date|after_or_equal:period_start',
        ]);

        $metrics = $this->wageService->getPerformanceMetrics(
            $user->id,
            $request->period_start,
            $request->period_end
        );

        return response()->json($metrics);
    }

    public function calculateWage(Request $request, User $user)
    {
        $request->validate([
            'period_start' => 'required|date',
            'period_end' => 'required|date|after_or_equal:period_start',
        ]);

        $wage = $this->wageService->calculateWage(
            $user->id,
            $request->period_start,
            $request->period_end
        );

        return response()->json($wage);
    }

    public function storeWage(Request $request, User $user)
    {
        $data = $request->validate([
            'period_start' => 'required|date',
            'period_end' => 'required|date|after_or_equal:period_start',
            'sessions_count' => 'required|integer|min:0',
            'hours_worked' => 'required|numeric|min:0',
            'base_amount' => 'required|numeric|min:0',
            'bonus' => 'nullable|numeric|min:0',
            'deductions' => 'nullable|numeric|min:0',
            'total_amount' => 'required|numeric|min:0',
            'currency_id' => 'required|exists:currencies,id',
            'notes' => 'nullable|string',
        ]);

        $data['user_id'] = $user->id;

        $wage = $this->wageService->createWage($data, auth()->id());

        return response()->json([
            'message' => 'Wage record created successfully',
            'wage' => $wage,
        ]);
    }
}
