<?php

namespace App\Http\Controllers\Agency\Product;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

abstract class BaseProductController extends Controller
{
    /**
     * Her ürün türü kendi model'ini döner
     */
    abstract protected function getModel(): string;

    /**
     * Her ürün türü kendi product_type_id'sini döner
     */
    abstract protected function getProductTypeId(): int;

    /**
     * Her ürün türü kendi view path'ini döner
     */
    abstract protected function getViewPath(): string;

    /**
     * Her ürün türü kendi route prefix'ini döner
     */
    abstract protected function getRoutePrefix(): string;

    /**
     * Ortak index metodu
     */
    public function index(Request $request)
    {
        $model = $this->getModel();
        $query = $model::where('agency_id', auth()->user()->agency_id)
            ->with(['product', 'product.categories']);

        // Search
        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                    ->orWhere('code', 'like', "%{$search}%")
                    ->orWhere('description', 'like', "%{$search}%");
            });
        }

        // Status filter
        if ($request->filled('status')) {
            if ($request->status === 'active') {
                $query->where('is_active', true);
            } elseif ($request->status === 'inactive') {
                $query->where('is_active', false);
            }
        }

        // Category filter
        if ($request->filled('category_id')) {
            $query->whereHas('product.categories', function ($q) use ($request) {
                $q->where('product_categories.id', $request->category_id);
            });
        }

        // Sort
        $sortBy = $request->get('sort_by', 'created_at');
        $sortOrder = $request->get('sort_order', 'desc');
        $query->orderBy($sortBy, $sortOrder);

        $products = $query->paginate(20)->withQueryString();

        return view($this->getViewPath() . '.index', compact('products'));
    }

    /**
     * Ortak toggle status metodu
     */
    public function toggleStatus($id)
    {
        $model = $this->getModel();
        $item = $model::where('agency_id', auth()->user()->agency_id)->findOrFail($id);

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

        return response()->json([
            'success' => true,
            'message' => 'Durum başarıyla güncellendi.',
            'is_active' => $item->is_active
        ]);
    }

    /**
     * Ortak publish metodu
     */
    public function publish($id)
    {
        $model = $this->getModel();
        $item = $model::where('agency_id', auth()->user()->agency_id)->findOrFail($id);

        $item->is_active = true;
        $item->is_bookable = true;
        $item->published_at = now();
        $item->save();

        return response()->json([
            'success' => true,
            'message' => 'Ürün başarıyla yayınlandı.'
        ]);
    }

    /**
     * Ortak destroy metodu
     */
    public function destroy($id)
    {
        $model = $this->getModel();
        $item = $model::where('agency_id', auth()->user()->agency_id)->findOrFail($id);

        $item->delete();

        return redirect()->route($this->getRoutePrefix() . '.index')
            ->with('success', 'Ürün başarıyla silindi.');
    }
}
