<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\Technology;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ProjectController extends Controller
{
    public function index(Request $request)
    {
        $query = Project::with('technologies');

        // Search by Project Name
        if ($request->filled('search')) {
            $query->where(
                'project_name',
                'like',
                '%' . $request->search . '%'
            );
        }

        // Search by Technology
        if ($request->filled('technology')) {
            $query->whereHas('technologies', function ($q) use ($request) {
                $q->where('technologies.id', $request->technology);
            });
        }

        $projects = $query->latest()->get();

        $technologies = Technology::all();

        return view(
            'projects.index',
            compact('projects', 'technologies')
        );
    }

    public function create()
    {
        $technologies = Technology::withCount('projects')->get();

        return view(
            'projects.create',
            compact('technologies')
        );
    }

    public function store(Request $request)
    {
        $request->validate([
            'project_name' => 'required|string|max:255',
            'short_description' => 'required|string',
            'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
            'github_link' => 'nullable|url',
            'live_demo_link' => 'nullable|url',
            'status' => 'required|string',
            'technologies' => 'required|array',
        ]);

        $imagePath = null;

        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')
                ->store('projects', 'public');
        }

        $project = Project::create([
            'project_name' => $request->project_name,
            'short_description' => $request->short_description,
            'image' => $imagePath,
            'github_link' => $request->github_link,
            'live_demo_link' => $request->live_demo_link,
            'status' => $request->status,
        ]);

        $project->technologies()
                ->attach($request->technologies);

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

    public function show($id)
    {
        $project = Project::with('technologies')
                    ->findOrFail($id);

        return view(
            'projects.show',
            compact('project')
        );
    }

    public function edit($id)
    {
        $project = Project::with('technologies')
                    ->findOrFail($id);

        $technologies = Technology::all();

        return view(
            'projects.edit',
            compact(
                'project',
                'technologies'
            )
        );
    }

    public function update(Request $request, $id)
    {
        $project = Project::findOrFail($id);

        $request->validate([
            'project_name' => 'required|string|max:255',
            'short_description' => 'required|string',
            'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
            'github_link' => 'nullable|url',
            'live_demo_link' => 'nullable|url',
            'status' => 'required|string',
            'technologies' => 'required|array',
        ]);

        $imagePath = $project->image;

        if ($request->hasFile('image')) {

            if (
                !empty($project->image)
                && Storage::disk('public')
                    ->exists($project->image)
            ) {
                Storage::disk('public')
                    ->delete($project->image);
            }

            $imagePath = $request->file('image')
                ->store('projects', 'public');
        }

        $project->update([
            'project_name' => $request->project_name,
            'short_description' => $request->short_description,
            'image' => $imagePath,
            'github_link' => $request->github_link,
            'live_demo_link' => $request->live_demo_link,
            'status' => $request->status,
        ]);

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

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

    public function destroy($id)
    {
        $project = Project::findOrFail($id);

        if (
            !empty($project->image)
            && Storage::disk('public')
                ->exists($project->image)
        ) {
            Storage::disk('public')
                ->delete($project->image);
        }

        $project->technologies()->detach();

        $project->delete();

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