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

use App\Http\Controllers\Controller;
use App\Models\Campaign;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class CampaignAdminController extends Controller
{
    public function index()
    {
        $campaigns = Campaign::orderByDesc('created_at')->paginate(15);
        return view('admin.campaigns.index', compact('campaigns'));
    }

    public function create()
    {
        return view('admin.campaigns.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255|unique:campaigns,slug',
            'objective' => 'nullable|string|max:500',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
            'status' => 'nullable|in:draft,published,archived',
            'starts_at' => 'nullable|date',
            'ends_at' => 'nullable|date',
            'priority' => 'nullable|integer|min:0',
            'is_pinned' => 'boolean',
            'seo_title' => 'nullable|string|max:70',
            'seo_description' => 'nullable|string|max:160',
            'seo_keywords' => 'nullable|string|max:255',
            'seo_canonical' => 'nullable|string|max:255',
            'seo_og_title' => 'nullable|string|max:70',
            'seo_og_description' => 'nullable|string|max:160',
            'seo_twitter_title' => 'nullable|string|max:70',
            'seo_twitter_description' => 'nullable|string|max:160',
        ]);

        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('campaigns', 'public');
        }

        $data['is_pinned'] = $request->boolean('is_pinned');

        Campaign::create($data);

        return redirect()->route('admin.campaigns.index')->with('success', 'Campanha criada com sucesso.');
    }

    public function edit($id)
    {
        $campaign = Campaign::findOrFail($id);
        return view('admin.campaigns.edit', compact('campaign'));
    }

    public function update(Request $request, $id)
    {
        $campaign = Campaign::findOrFail($id);

        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255|unique:campaigns,slug,' . $id,
            'objective' => 'nullable|string|max:500',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
            'status' => 'nullable|in:draft,published,archived',
            'starts_at' => 'nullable|date',
            'ends_at' => 'nullable|date',
            'priority' => 'nullable|integer|min:0',
            'is_pinned' => 'boolean',
            'seo_title' => 'nullable|string|max:70',
            'seo_description' => 'nullable|string|max:160',
            'seo_keywords' => 'nullable|string|max:255',
            'seo_canonical' => 'nullable|string|max:255',
            'seo_og_title' => 'nullable|string|max:70',
            'seo_og_description' => 'nullable|string|max:160',
            'seo_twitter_title' => 'nullable|string|max:70',
            'seo_twitter_description' => 'nullable|string|max:160',
        ]);

        if ($request->hasFile('image')) {
            if ($campaign->image) {
                Storage::disk('public')->delete($campaign->image);
            }
            $data['image'] = $request->file('image')->store('campaigns', 'public');
        }

        $data['is_pinned'] = $request->boolean('is_pinned');

        $campaign->update($data);

        return redirect()->route('admin.campaigns.index')->with('success', 'Campanha atualizada com sucesso.');
    }

    public function destroy($id)
    {
        $campaign = Campaign::findOrFail($id);

        if ($campaign->image) {
            Storage::disk('public')->delete($campaign->image);
        }

        $campaign->delete();

        return redirect()->route('admin.campaigns.index')->with('success', 'Campanha removida com sucesso.');
    }
}
