<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\AdCampaign;
use App\Services\MarketingIntelligenceService;
use Illuminate\Http\Request;

class MarketingIntelligenceController extends Controller
{
    protected $service;

    public function __construct(MarketingIntelligenceService $service)
    {
        $this->service = $service;

        $this->middleware('permission:operations.marketing_intelligence.view|operations.marketing_intelligence.performance_insights.view')
            ->only(['performanceInsights']);
        $this->middleware('permission:operations.marketing_intelligence.view|operations.marketing_intelligence.optimize_budget.view')
            ->only(['optimizeBudget']);
        $this->middleware('permission:operations.marketing_intelligence.view|operations.marketing_intelligence.client_report.view')
            ->only(['clientReport']);
        $this->middleware('permission:operations.marketing_intelligence.view|operations.marketing_intelligence.suggest_interests.view')
            ->only(['suggestInterests']);
        $this->middleware('permission:operations.marketing_intelligence.view|operations.marketing_intelligence.suggest_ad_copies.view')
            ->only(['suggestAdCopies']);
    }

    public function performanceInsights(Request $request)
    {
        $request->validate([
            'campaign_id' => 'required|exists:ad_campaigns,id',
        ]);

        $campaign = AdCampaign::findOrFail($request->campaign_id);
        $insights = $this->service->analyzePerformance($campaign);

        return response()->json($insights);
    }

    public function optimizeBudget(Request $request)
    {
        $request->validate([
            'campaign_id' => 'required|exists:ad_campaigns,id',
        ]);

        $campaign = AdCampaign::findOrFail($request->campaign_id);
        $recommendations = $this->service->optimizeBudget($campaign);

        return response()->json(['suggestions' => $recommendations]);
    }

    public function clientReport(Request $request)
    {
        $request->validate([
            'campaign_id' => 'required|exists:ad_campaigns,id',
        ]);

        $campaign = AdCampaign::findOrFail($request->campaign_id);
        $language = $request->input('language', 'ar');
        $report = $this->service->generateClientReport($campaign, $language);

        return response()->json(['report' => $report]);
    }

    public function suggestInterests(Request $request)
    {
        $request->validate([
            'business_type' => 'required|string',
            'location' => 'nullable|string',
        ]);

        $interests = $this->service->suggestInterests(
            $request->business_type,
            $request->location ?? 'General'
        );

        return response()->json(['interests' => $interests]);
    }

    public function suggestAdCopies(Request $request)
    {
        $request->validate([
            'business_name' => 'required|string',
            'business_type' => 'required|string',
            'objective' => 'required|string',
            'language' => 'nullable|string|in:ar,en',
        ]);

        $copies = $this->service->suggestAdCopies(
            $request->business_name,
            $request->business_type,
            $request->objective,
            $request->language ?? 'ar'
        );

        return response()->json(['suggestions' => $copies]);
    }
}
