<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Album;
use Illuminate\Support\Facades\File;

class AlbumController extends Controller
{
    public function allAlbum()
    {
       
        $data = [
            $pageTitle = 'Album',
            $albums = Album::withCount('photos')->latest()->get()
        ];
        return view('back.pages.album.album', $data);
    }

    // Show form to add new album
    public function addAlbum()
    {
        $data = [
            $pageTitle = 'Add Album'
        ];
        return view('back.pages.album.add_album', $data);
    }
    public function createAlbum(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'cover_photo' => 'nullable|image|mimes:jpg,jpeg,png|max:4096',
        ]);

        $filename = null;

        if ($request->hasFile('cover_photo')) {
            $path = "images/albums/";
            $file = $request->file('cover_photo');
            $originalName = $file->getClientOriginalName();
            $newFilename = time() . '_' . $originalName;

            $upload = $file->move(public_path($path), $newFilename);

            if ($upload) {
                $album = new Album();
                $album->title = $request->title;
                $album->description = $request->description;
                $album->cover_photo = $newFilename;
                $album->is_published = $request->has('is_published');

                $saved = $album->save();

                if ($saved) {
                    return response()->json(['status' => 1, 'message' => 'Album has been successfully created.']);
                } else {
                    return response()->json(['status' => 0, 'message' => 'Something went wrong while saving the album.']);
                }
            } else {
                return response()->json(['status' => 0, 'message' => 'Failed to upload cover photo.']);
            }
        } else {
            // Handle case where no cover photo is uploaded
            $album = new Album();
            $album->title = $request->title;
            $album->description = $request->description;
            $album->is_published = $request->has('is_published');

            $saved = $album->save();

            if ($saved) {
                return response()->json(['status' => 1, 'message' => 'Album has been created without a cover photo.']);
            } else {
                return response()->json(['status' => 0, 'message' => 'Something went wrong while saving the album.']);
            }
        }
    }
    public function editAlbum($id)
    {
        $album = Album::findOrFail($id);
        return view('back.pages.album.edit_album', compact('album'));
    }

    public function updateAlbum(Request $request)
{
    $album = Album::findOrFail($request->id);

    // Validate fields
    $request->validate([
        'title' => 'required|string|max:255',
        'description' => 'nullable|string',
        'cover_photo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:4096', // 2MB Max
    ]);

    try {
        $album->title = $request->title;
        $album->description = $request->description;
        $album->is_published = $request->has('is_published') ? 1 : 0;

        // If new cover photo uploaded
        if ($request->hasFile('cover_photo')) {
            $path = 'images/albums/';

            // Delete old cover photo if exists
            if ($album->cover_photo && File::exists(public_path($path . $album->cover_photo))) {
                File::delete(public_path($path . $album->cover_photo));
            }

            // Upload new one
            $file = $request->file('cover_photo');
            $fileName = uniqid() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path($path), $fileName);

            $album->cover_photo = $fileName;
        }

        $album->save();

        return response()->json([
            'status' => 1,
            'message' => 'Album updated successfully!'
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'status' => 0,
            'message' => 'Something went wrong. ' . $e->getMessage()
        ]);
    }
}

}
