<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Investor;
use App\Services\InvestorService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class InvestorController extends Controller
{
    public function __construct(
        protected InvestorService $investorService
    ) {}

    /**
     * Get investor profile
     * GET /api/investors/{id}
     */
    public function show(string $id): JsonResponse
    {
        $investor = Investor::with(['investments' => function ($query) {
            $query->orderByDesc('created_at')->limit(10);
        }])->findOrFail($id);

        return response()->json([
            'success' => true,
            'data' => $investor,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Update investor profile
     * PUT /api/investors/{id}
     */
    public function update(Request $request, string $id): JsonResponse
    {
        $validated = $request->validate([
            'full_name' => 'sometimes|string|max:255',
            'phone' => 'sometimes|nullable|string|max:50',
            'country' => 'sometimes|nullable|string|max:100',
            'risk_profile' => 'sometimes|nullable|in:conservative,moderate,aggressive',
            'communication_preferences' => 'sometimes|nullable|array',
        ]);

        $investor = Investor::findOrFail($id);
        $investor = $this->investorService->updateProfile($investor, $validated);

        return response()->json([
            'success' => true,
            'data' => $investor,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Get investor by email
     * GET /api/investors/email/{email}
     */
    public function showByEmail(string $email): JsonResponse
    {
        $investor = Investor::where('email', strtolower($email))->firstOrFail();

        return response()->json([
            'success' => true,
            'data' => $investor,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Get all investments for an investor
     * GET /api/investors/{id}/investments
     */
    public function investments(string $id): JsonResponse
    {
        $investor = Investor::findOrFail($id);
        $history = $this->investorService->getInvestmentHistory($investor);

        return response()->json([
            'success' => true,
            'data' => $history,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Get returns for an investor
     * GET /api/investors/{id}/returns
     */
    public function returns(string $id): JsonResponse
    {
        $investor = Investor::findOrFail($id);
        $returns = $this->investorService->calculateReturns($investor);

        return response()->json([
            'success' => true,
            'data' => $returns,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Get dividend history for an investor
     * GET /api/investors/{id}/dividends
     */
    public function dividends(string $id): JsonResponse
    {
        $investor = Investor::findOrFail($id);
        $dividends = $this->investorService->getDividendHistory($investor);

        return response()->json([
            'success' => true,
            'data' => $dividends,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }
}
