<?php

namespace App\Http\Controllers\Api;

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

class AdminBannerController extends Controller
{
    public function index()
    {
        try {
            $banners = Banner::orderBy('sequence', 'asc')->get();
            return response()->json(['success' => true, 'data' => $banners]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }

    public function store(Request $request)
    {
        $user = $request->user();
        
        // PENAMBAHAN: Proteksi Hak Akses Pusat (Banner bersifat Global untuk PWA)
        if ($user && $user->role !== 'owner') {
            return response()->json([
                'success' => false, 
                'message' => 'Akses ditolak: Hanya Owner yang berhak menambah Banner Aplikasi.'
            ], 403);
        }

        $request->validate([
            'title' => 'required|string|max:255',
            'bg_color' => 'required|string',
            'link_type' => 'required|in:product,category,global',
            'image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048'
        ]);

        try {
            $imagePath = null;
            if ($request->hasFile('image')) {
                $imagePath = $request->file('image')->store('banners', 'public');
            }

            $banner = Banner::create([
                'title' => $request->title,
                'subtitle' => $request->subtitle,
                'bg_color' => $request->bg_color,
                'link_type' => $request->link_type,
                'link_id' => $request->link_id,
                'sequence' => $request->sequence ?? 0,
                'is_active' => $request->is_active ?? true,
                'image_path' => $imagePath
            ]);

            return response()->json(['success' => true, 'message' => 'Banner Berhasil Disimpan', 'data' => $banner]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }

    public function update(Request $request, $id)
    {
        $user = $request->user();
        
        // PENAMBAHAN: Proteksi Hak Akses Pusat
        if ($user && $user->role !== 'owner') {
            return response()->json([
                'success' => false, 
                'message' => 'Akses ditolak: Hanya Owner yang berhak mengubah Banner Aplikasi.'
            ], 403);
        }

        try {
            $banner = Banner::findOrFail($id);
            
            if ($request->hasFile('image')) {
                if ($banner->image_path) {
                    Storage::disk('public')->delete($banner->image_path);
                }
                $banner->image_path = $request->file('image')->store('banners', 'public');
            }

            $banner->update($request->only(['title', 'subtitle', 'bg_color', 'link_type', 'link_id', 'sequence', 'is_active']));

            return response()->json(['success' => true, 'message' => 'Banner Berhasil Diperbarui']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }

    public function destroy(Request $request, $id)
    {
        $user = $request->user();
        
        // PENAMBAHAN: Proteksi Hak Akses Pusat
        if ($user && $user->role !== 'owner') {
            return response()->json([
                'success' => false, 
                'message' => 'Akses ditolak: Hanya Owner yang berhak menghapus Banner Aplikasi.'
            ], 403);
        }

        try {
            $banner = Banner::findOrFail($id);
            if ($banner->image_path) {
                Storage::disk('public')->delete($banner->image_path);
            }
            $banner->delete();
            return response()->json(['success' => true, 'message' => 'Banner Berhasil Dihapus']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }
}