<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Listing;
use App\Models\ListingVideo;
use App\Services\ListingVideoService;
use Illuminate\Http\Request;

/**
 * Admin ilan videosu yönetimi: sahiplik kontrolü YOK — yetki route'taki
 * permission:listing.approve ile sağlanır (Admin\ListingMediaController deseni).
 */
class ListingVideoController extends Controller
{
    public function __construct(private ListingVideoService $videos)
    {
    }

    public function store(Request $request, Listing $listing)
    {
        $data = $request->validate(ListingVideoService::RULES);

        $video = $this->videos->store(
            $request,
            $listing,
            $request->file('video'),
            $data['title'] ?? null,
            isset($data['duration_seconds']) ? (int) $data['duration_seconds'] : null,
        );

        return response()->json(['data' => $this->videos->toArray($video)], 201);
    }

    public function destroy(Listing $listing, ListingVideo $video)
    {
        abort_unless($video->listing_id === $listing->id, 404, 'Video bulunamadı.');

        $this->videos->delete($video);

        return response()->json(['message' => 'Video silindi.']);
    }
}
