<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Project;
use App\Models\ProjectTask;
use App\Models\ProjectMember;
use Illuminate\Support\Facades\Auth;

class ProjectController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        if ($user->hasRole(['admin', 'super_admin'])) {
            $projects = Project::with(['department', 'members'])->paginate(12);
        } else {
            $projects = $user->projects()->with(['department', 'members'])->paginate(12);
        }
        
        return view('projects.index', compact('projects'));
    }

    public function show(Project $project)
    {
        $user = Auth::user();
        // Check access
        if (!$user->projects()->where('project_id', $project->id)->exists() && !$user->hasRole(['admin', 'super_admin'])) {
            abort(403);
        }

        $project->load(['members', 'tasks.assignee']);
        
        $tasks = [
            'todo' => $project->tasks()->where('status', 'todo')->orderBy('order')->get(),
            'in_progress' => $project->tasks()->where('status', 'in_progress')->orderBy('order')->get(),
            'in_review' => $project->tasks()->where('status', 'in_review')->orderBy('order')->get(),
            'done' => $project->tasks()->where('status', 'done')->orderBy('order')->get(),
        ];

        return view('projects.show', compact('project', 'tasks'));
    }
    public function create()
    {
        $departments = \App\Models\Department::all();
        return view('admin.projects.create', compact('departments'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'department_id' => 'required|exists:departments,id',
            'start_date' => 'required|date',
            'end_date' => 'nullable|date|after_or_equal:start_date',
            'status' => 'required|in:active,on_hold,completed,cancelled',
        ]);

        $project = Project::create($validated);

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

    public function edit(Project $project)
    {
        $departments = \App\Models\Department::all();
        return view('admin.projects.edit', compact('project', 'departments'));
    }

    public function update(Request $request, Project $project)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'department_id' => 'required|exists:departments,id',
            'start_date' => 'required|date',
            'end_date' => 'nullable|date|after_or_equal:start_date',
            'status' => 'required|in:active,on_hold,completed,cancelled',
        ]);

        $project->update($validated);

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

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