<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\ProjectVariation;
use App\Models\ProjectAuditLog;
use Illuminate\Http\Request;

class ProjectVariationController extends Controller
{
    public function listVariations($projectId)
    {
        $project = Project::findOrFail($projectId);
        
        return response()->json([
            'variations' => $project->variations()->with('raisedBy')->latest()->get(),
        ]);
    }

    public function createVariation($projectId, Request $request)
    {
        $project = Project::findOrFail($projectId);

        $validated = $request->validate([
            'name' => 'required|string',
            'description' => 'required|string',
            'cost_impact' => 'required|numeric',
            'reason' => 'nullable|string',
        ]);

        $variation = $project->variations()->create([
            ...$validated,
            'raised_by' => auth()->id(),
            'raised_at' => now(),
            'status' => 'pending',
        ]);

        ProjectAuditLog::log(
            $project,
            'variation_raised',
            "Variation '{$variation->name}' has been raised (Cost Impact: {$variation->cost_impact})",
            auth()->user(),
            'variation',
            $variation->id
        );

        return response()->json(['success' => true, 'variation' => $variation], 201);
    }

    public function approveVariation($projectId, $variationId, Request $request)
    {
        $variation = ProjectVariation::where('project_id', $projectId)
            ->findOrFail($variationId);

        $validated = $request->validate([
            'notes' => 'nullable|string',
        ]);

        $variation->approve(auth()->user(), $validated['notes'] ?? null);

        ProjectAuditLog::log(
            $variation->project,
            'variation_approved',
            "Variation '{$variation->name}' has been approved by {$variation->approvals[auth()->user()->role]['approved_by']}",
            auth()->user(),
            'variation',
            $variation->id
        );

        return response()->json(['success' => true, 'variation' => $variation]);
    }

    public function rejectVariation($projectId, $variationId, Request $request)
    {
        $variation = ProjectVariation::where('project_id', $projectId)
            ->findOrFail($variationId);

        $validated = $request->validate([
            'reason' => 'required|string',
        ]);

        $variation->reject(auth()->user(), $validated['reason']);

        ProjectAuditLog::log(
            $variation->project,
            'variation_rejected',
            "Variation '{$variation->name}' has been rejected",
            auth()->user(),
            'variation',
            $variation->id
        );

        return response()->json(['success' => true, 'variation' => $variation]);
    }

    public function implementVariation($projectId, $variationId)
    {
        $variation = ProjectVariation::where('project_id', $projectId)
            ->findOrFail($variationId);

        if ($variation->status !== 'approved') {
            return response()->json([
                'success' => false,
                'message' => 'Only approved variations can be implemented',
            ], 422);
        }

        $variation->update([
            'status' => 'implemented',
            'implemented_at' => now(),
        ]);

        ProjectAuditLog::log(
            $variation->project,
            'variation_implemented',
            "Variation '{$variation->name}' has been implemented",
            auth()->user(),
            'variation',
            $variation->id
        );

        return response()->json(['success' => true, 'variation' => $variation]);
    }
}
