<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\{Project, Client, User, ActivityLog};
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    public function index(Request $request)
    {
        $q = Project::with(['client','pic'])->orderByDesc('created_at');
        if ($request->status) $q->where('status', $request->status);
        $projects = $q->paginate(20)->withQueryString();
        return view('admin.projects.index', compact('projects'));
    }

    public function create()
    {
        $clients = Client::where('is_active',1)->orderBy('name')->get();
        $users   = User::where('is_active',1)->get();
        return view('admin.projects.create', compact('clients','users'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'client_id'   => 'required|exists:clients,id',
            'pic_id'      => 'nullable|exists:users,id',
            'title'       => 'required|string',
            'description' => 'nullable|string',
            'rab_amount'  => 'nullable|numeric',
            'start_date'  => 'nullable|date',
            'end_date'    => 'nullable|date',
        ]);
        $data['project_number'] = Project::generateNumber();
        $project = Project::create($data);
        ActivityLog::log('project','created',"Proyek #{$project->project_number} dibuat", $project);
        return redirect()->route('admin.projects.show', $project)->with('success','Proyek dibuat.');
    }

    public function show(Project $project)
    {
        $project->load(['client','pic']);
        return view('admin.projects.show', compact('project'));
    }

    public function edit(Project $project)
    {
        $clients = Client::where('is_active',1)->orderBy('name')->get();
        $users   = User::where('is_active',1)->get();
        return view('admin.projects.edit', compact('project','clients','users'));
    }

    public function update(Request $request, Project $project)
    {
        $data = $request->validate([
            'title'        => 'required',
            'description'  => 'nullable|string',
            'rab_amount'   => 'nullable|numeric',
            'final_amount' => 'nullable|numeric',
            'start_date'   => 'nullable|date',
            'end_date'     => 'nullable|date',
            'pic_id'       => 'nullable|exists:users,id',
        ]);
        $project->update($data);
        return redirect()->route('admin.projects.show', $project)->with('success','Proyek diperbarui.');
    }

    public function changeStatus(Request $request, Project $project)
    {
        $request->validate(['status'=>'required|in:survey,rab_submitted,approved,in_progress,testing,done,cancelled']);
        $project->update(['status'=>$request->status]);
        return back()->with('success','Status proyek diperbarui.');
    }

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