<?php

namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;

use App\Models\Project;
use App\Models\Label;
use App\Models\Client;
use App\Models\Status;
use App\Models\Milestone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; 

class ClientProjectController extends Controller
{
    public function index(Request $request)
    {
        // Get the authenticated user's client_id
        $user = Auth::user();
        $client_id = $user->client_id; 
    
        // Start the project query with a client_id filter
        $projectsQuery = Project::with('labels', 'client')
                                ->where('client_id', $client_id); // Filter by client_id
    
        // Apply label filter if provided
        if ($request->filled('label_id')) {
            $projectsQuery->whereHas('labels', function ($query) use ($request) {
                $query->where('label_id', $request->label_id);
            });
        }
    
        // Apply start date filter if provided
        if ($request->filled('start_date')) {
            $projectsQuery->where('start_date', '>=', $request->start_date);
        }
    
        // Apply deadline filter if provided
        if ($request->filled('deadline')) {
            $projectsQuery->where('deadline', '<=', $request->deadline);
        }
    
        // Get the filtered projects
        $projects = $projectsQuery->get();
        $labels = Label::all();
        $clients = Client::all();
        $statuses = Status::all();
    
        // Return the view with filtered projects and other data
        return view('client.projects.index', compact('projects', 'labels', 'clients', 'statuses'));
    }
    

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'start_date' => 'required|date',
            'deadline' => 'required|date',
            'price' => 'required|numeric',
            'labels' => 'array',
            'status_id' => 'nullable|exists:statuses,id',
        ]);

        $project = new Project();
        $project->title = $request->title;
        $project->client_id = $request->client_id;
        $project->description = $request->description;
        $project->start_date = $request->start_date;
        $project->deadline = $request->deadline;
        $project->price = $request->price;
        $project->status_id = $request->status_id;
        $project->save();

        $project->labels()->sync($request->labels);

        return redirect()->route('client.projects.index')->with('success', 'Project added successfully');
    }

    public function edit($id)
    {
        // Fetch the project with related labels, client, tasks (and their employees), and milestones
        $project = Project::with(['labels', 'client', 'tasks.employees'])->findOrFail($id);
        $labels = Label::all();
        $clients = Client::all();
        $statuses = Status::all();
        $milestones = Milestone::where('project_id', $id)->get();
        // Pass the project data to the view
        return view('client.projects.edit', compact('project', 'labels', 'clients', 'statuses', 'milestones'));
    }
    


    public function update(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'start_date' => 'required|date',
            'deadline' => 'required|date',
            'price' => 'required|numeric',
            'labels' => 'array',
            'status_id' => 'nullable|exists:statuses,id',
        ]);

        $project = Project::findOrFail($id);
        $project->title = $request->title;
        $project->client_id = $request->client_id;
        $project->description = $request->description;
        $project->start_date = $request->start_date;
        $project->deadline = $request->deadline;
        $project->price = $request->price;
        $project->status_id = $request->status_id;
        $project->save();

        $project->labels()->sync($request->labels);

        return redirect()->route('client.projects.index')->with('success', 'Project updated successfully');
    }

    public function destroy($id)
    {
        $project = Project::findOrFail($id);
        $project->delete();
        return redirect()->route('client.projects.index')->with('success', 'Project deleted successfully');
    }
}
