<?php

namespace App\Http\Controllers\Api;

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

class AdvertisementController extends Controller
{
    public function index()
    {
        $advertisements = Advertisement::latest()->get();
        return response()->json([
            'success' => true,
            'data' => $advertisements
        ]);
    }

    public function active()
    {
        // Only return the active advertisements (or just the latest one if you only want one, but we'll return all active ones just in case)
        $advertisements = Advertisement::where('is_active', true)->latest()->get();
        return response()->json([
            'success' => true,
            'data' => $advertisements
        ]);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'nullable|string|max:255',
            'type' => 'required|in:image,video',
            'file' => 'required|file|max:51200', // max 50MB
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $validator->errors()
            ], 422);
        }

        if ($request->hasFile('file')) {
            $file = $request->file('file');
            
            // Generate unique filename
            $filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            
            // Use public disk for storage
            $path = $file->storeAs('advertisements', $filename, 'public');
            
            // Create advertisement record
            $advertisement = Advertisement::create([
                'title' => $request->title,
                'type' => $request->type,
                'file_path' => '/storage/' . $path,
                'is_active' => false // default inactive
            ]);

            return response()->json([
                'success' => true,
                'message' => 'Iklan berhasil ditambahkan',
                'data' => $advertisement
            ], 201);
        }

        return response()->json([
            'success' => false,
            'message' => 'Gagal mengupload file'
        ], 400);
    }

    public function destroy($id)
    {
        $advertisement = Advertisement::find($id);
        
        if (!$advertisement) {
            return response()->json([
                'success' => false,
                'message' => 'Iklan tidak ditemukan'
            ], 404);
        }

        // Delete file from storage
        $path = str_replace('/storage/', '', $advertisement->file_path);
        if (Storage::disk('public')->exists($path)) {
            Storage::disk('public')->delete($path);
        }

        $advertisement->delete();

        return response()->json([
            'success' => true,
            'message' => 'Iklan berhasil dihapus'
        ]);
    }

    public function toggleActive($id)
    {
        $advertisement = Advertisement::find($id);
        
        if (!$advertisement) {
            return response()->json([
                'success' => false,
                'message' => 'Iklan tidak ditemukan'
            ], 404);
        }

        // Optional: If you only want one active ad at a time, you can deactivate others here
        // Advertisement::where('id', '!=', $id)->update(['is_active' => false]);

        $advertisement->is_active = !$advertisement->is_active;
        $advertisement->save();

        return response()->json([
            'success' => true,
            'message' => 'Status iklan berhasil diubah',
            'data' => $advertisement
        ]);
    }
}
