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

use Illuminate\Http\Request;
use App\Models\BannerAd;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class BannerAdController extends Controller
{
    public function createBannerAds(Request $request)
    {
        
        // Create a new BannerAd instance
        $bannerAd = new BannerAd();

        // Assign request data to the model
        $bannerAd->title = $request->title;
        $bannerAd->description = $request->description;

        // Save the BannerAd model to get the auto-incremented ID from the database
        $bannerAd->save(); // Save the record initially to get the ID

        // Create directory for the banner ad based on the new ID
        $ID = $bannerAd->ID; // Use the ID of the saved record
        $bannerAdDir = 'storage/images/' . $ID ;

        if (!File::isDirectory($bannerAdDir)) {
            File::makeDirectory($bannerAdDir, 0755, true);
        }

        // If there's an image in the request, move it to the new directory
        if ($request->hasFile('image')) {
            $imageName = 'banner_image' . '.' . $request->file('image')->getClientOriginalExtension();
            $request->file('image')->move($bannerAdDir, $imageName);
            // Update the BannerAd with the image path
            $bannerAd->image = $bannerAdDir . '/' . $imageName; // Save the image path in the model
        }

        // Save the updated BannerAd with the image path
        $bannerAd->save(); // Save the model with the new image path

        // Redirect after saving
        return redirect('/contentManagement');
    
    }
    
    public function getAdvertisement($id) {
  $advertisement = BannerAd::find($id);
  if ($advertisement) {
    return response()->json([
      'title' => $advertisement->title,
      'description' => $advertisement->description,
      'image' => $advertisement->image,
    ]);
  } else {
    return response()->json(['error' => 'Advertisement not found'], 404);
  }
}

public function updateBannerAds(Request $request, $id)
{
    // Ensure correct ID is received
    \Log::info("Updating advertisement with ID: $id");

    // Find and update the existing BannerAd by ID
    $bannerAd = BannerAd::findOrFail($id);

    // Update text fields
    $bannerAd->title = $request->title;
    $bannerAd->description = $request->description;

    // If there's a new image in the request, move it to the correct directory and update the path
    if ($request->hasFile('image')) {
        // Create the directory if it doesn't exist
        $bannerAdDir = 'storage/images/' . $id;
        if (!File::isDirectory($bannerAdDir)) {
            File::makeDirectory($bannerAdDir, 0755, true);
        }

        // Name the image and move it
        $imageName = 'banner_image' . '.' . $request->file('image')->getClientOriginalExtension();

        // Delete the existing image if it's there
        if (File::exists($bannerAd->image)) {
            File::delete($bannerAd->image);
        }

        // Move the new image
        $request->file('image')->move($bannerAdDir, $imageName);

        // Update the image path in the database
        $bannerAd->image = $bannerAdDir . '/' . $imageName;
    }

    // Save updated record
    $bannerAd->update();

    // Redirect after updating
    return redirect('/contentManagement');
}

public function updateStatusAd(Request $request)
{
    $adId = $request->input('ID'); // Check if 'ID' is correctly retrieved
    $status = $request->input('status');

    if ($adId === null) {
        return response()->json(['success' => false, 'message' => 'ID is missing'], 400);
    }

    $ad = BannerAd::find($adId);
    if ($ad) {
        $ad->status = $status;
        $ad->save();
        return response()->json(['success' => true, 'message' => 'Status updated successfully']);
    } else {
        return response()->json(['success' => false, 'message' => 'Ad not found'], 404);
    }
}

    public function deleteAd($id)
    {
        // Find the ad by ID
        $ad = BannerAd::find($id);
        
        if ($ad) {
            // Get the directory where the images are stored
        $bannerAdDir = 'storage/images/' . $id;

        // Check if the directory exists
        if (File::isDirectory($bannerAdDir)) {
            // Delete the directory and all its contents
            File::deleteDirectory($bannerAdDir);
        }
            $ad->delete();
            return redirect()->back()->with('success', 'Ad deleted successfully');
        } else {
            return response()->json(['success' => false, 'message' => 'Ad not found'], 404);
        }
    }
}
