<?php

namespace App\Http\Controllers;

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

class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('project.index', [
            'projects' => Project::with('images')->get()
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('project.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'nama_aplikasi' => 'required|max:149',
            'deskripsi' => 'required',
            'link' => 'required|url:http,https'
        ]);

        try {
            $project = Project::create([
                'nama_aplikasi' => $request->nama_aplikasi,
                'deskripsi' => $request->deskripsi,
                'link' => $request->link
            ]);

            if ($request->hasFile('gambar')) {
                foreach ($request->file('gambar') as $file) {
                    $path = $file->store('projects', 'public');

                    ProjectImage::create([
                        'project_id' => $project->id,
                        'gambar' => $path
                    ]);
                }
            }

            return to_route('project.index')->with('success', 'Project berhasil ditambahkan');
        } catch (\Exception $e) {
            return to_route('project.index')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        try {
            $project = Project::with('images')->findOrFail($id);
            return view('project.show', [
                'project' => $project
            ]);
        } catch (\Exception $e) {
            return to_route('projects.index')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        try {
            $project = Project::with('images')->findOrFail($id);
            return view('project.edit', [
                'project' => $project
            ]);
        } catch (\Exception $e) {
            return to_route('projects.index')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        try {
            $project = Project::findOrFail($id);

            $project->update([
                'nama_aplikasi' => $request->nama_aplikasi,
                'deskripsi' => $request->deskripsi,
                'link' => $request->link
            ]);

            // hapus gambar lama
            if ($request->hapus_gambar) {
                foreach ($request->hapus_gambar as $img_id) {
                    $image = ProjectImage::find($img_id);

                    Storage::disk('public')->delete($image->gambar);

                    $image->delete();
                }
            }

            // upload gambar baru
            if ($request->hasFile('gambar')) {
                foreach ($request->file('gambar') as $file) {
                    $path = $file->store('projects', 'public');

                    ProjectImage::create([
                        'project_id' => $project->id,
                        'gambar' => $path
                    ]);
                }
            }

            return redirect()->route('project.index')
                ->with('success', 'Project berhasil diupdate');
        } catch (\Exception $e) {
            return to_route('projects.index')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $project = Project::with('images')->findOrFail($id);

            foreach ($project->images as $img) {
                Storage::disk('public')->delete($img->gambar);
                $img->delete();
            }

            $project->delete();

            return to_route('project.index')
                ->with('success', 'Project berhasil dihapus');
        } catch (\Exception $e) {
            return to_route('projects.index')->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
        }
    }

    // Hapus satu gambar
    public function deleteImage($id)
    {

        $image = ProjectImage::findOrFail($id);

        Storage::disk('public')->delete($image->gambar);

        $image->delete();

        return back()->with('success', 'Gambar berhasil dihapus');
    }
}
