<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\BannerAdRequest;

class BannerAdsController extends Controller
{
    // Display the banner ads management view
    public function index()
    {
        $bannerAds = DB::table('bannerads')->get();
        return view('bannerads.index', compact('bannerAds'));
    }

    // Store a new banner ad
    public function store(BannerAdRequest $request)
{
    try {
        // Store the image
        $path = $request->file('image')->store('bannerads', 'public');

        // Insert data into the database
        DB::table('bannerads')->insert([
            'image' => $path,
            'title' => $request->input('title'),
            'description' => $request->input('description'),
            'status' => $request->input('status', 'inactive'),
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return redirect()->route('contentManagement')->with('success', 'Banner ad created successfully.');
    } catch (\Exception $e) {
        \Log::error($e);
        return redirect()->route('contentManagement')->with('error', 'Failed to create banner ad.');
    }
}


    // Find a banner ad by ID
    private function findBannerAd($id)
    {
        return DB::table('bannerads')->where('id', $id)->first();
    }

    // Display a specific banner ad
    public function show($id)
    {
        $bannerAd = $this->findBannerAd($id);
        return $bannerAd ? response()->json($bannerAd) : response()->json(['error' => 'Banner ad not found.'], 404);
    }

// Update an existing banner ad
public function update(BannerAdRequest $request, $id)
{
    \Log::info('Update request data for banner ad ID ' . $id . ': ', $request->all());

    $bannerAd = $this->findBannerAd($id);
    if (!$bannerAd) {
        return redirect()->route('bannerads.index')->with('error', 'Banner ad not found.');
    }

    try {
        $updateData = [];

        if ($request->filled('title')) {
            $updateData['title'] = $request->input('title');
        }

        if ($request->filled('description')) {
            $updateData['description'] = $request->input('description');
        }

        if ($request->filled('status')) {
            $updateData['status'] = $request->input('status');
        }

        if ($request->hasFile('image')) {
            if (Storage::disk('public')->exists($bannerAd->image)) {
                Storage::disk('public')->delete($bannerAd->image);
            }
            $updateData['image'] = $request->file('image')->store('bannerads', 'public');
        }

        $updateData['updated_at'] = now();
        DB::table('bannerads')->where('id', $id)->update($updateData);

        return redirect()->route('contentManagement')->with('success', 'Banner ad updated successfully.');
    } catch (\Exception $e) {
        \Log::error('Error updating banner ad: ' . $e->getMessage());
        return redirect()->route('contentManagement')->with('error', 'Failed to update banner ad.');
    }
}






    // Delete a banner ad
    public function destroy($id)
{
    $bannerAd = $this->findBannerAd($id);
    if ($bannerAd) {
        try {
            Storage::disk('public')->delete($bannerAd->image);
            DB::table('bannerads')->where('id', $id)->delete();
            return redirect()->route('contentManagement')->with('success', 'Banner ad deleted successfully.');
        } catch (\Exception $e) {
            \Log::error($e);
            return redirect()->route('contentManagement')->with('error', 'Failed to delete banner ad.');
        }
    }
    return redirect()->route('contentManagement')->with('error', 'Banner ad not found.');
}


    // Get all active banner ads
    public function getActiveAds()
    {
        $activeAds = DB::table('bannerads')->where('status', 'active')->get();
        return response()->json($activeAds);
    }
}
