<?php

namespace App\Controller;

use App\Entity\Company;
use App\Entity\ResearchReport;
use App\Form\CompanySearchType;
use App\Form\CompanyType;
use App\Form\ResearchReportType;
use App\Repository\CompanyRepository;
use App\Repository\ResearchReportRepository;
use App\Service\LinkedInService;
use App\Service\NeuronAiService;
use App\Service\ReportExportService;
use App\Service\StockDataService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/company')]
class CompanyController extends AbstractController
{
    #[Route('/', name: 'company_index', methods: ['GET'])]
    public function index(CompanyRepository $companyRepository): Response
    {
        return $this->render('company/index.html.twig', [
            'companies' => $companyRepository->findAll(),
        ]);
    }

    #[Route('/search', name: 'company_search', methods: ['GET', 'POST'])]
    public function search(Request $request, CompanyRepository $companyRepository, StockDataService $stockDataService): Response
    {
        $form = $this->createForm(CompanySearchType::class);
        $form->handleRequest($request);

        $dbResults = [];
        $apiResults = [];
        $searchTerm = '';

        if ($form->isSubmitted() && $form->isValid()) {
            $searchTerm = $form->get('searchTerm')->getData();

            // First search in local database
            $dbResults = $companyRepository->findBySearchCriteria($searchTerm);

            // If no local results or explicitly searching by ticker
            if (count($dbResults) === 0 || preg_match('/^[A-Za-z]{1,5}$/', $searchTerm)) {
                try {
                    // Search in external APIs
                    $apiResults = $stockDataService->searchCompanies($searchTerm);

                    // Filter out companies that already exist in DB results
                    $existingSymbols = array_map(function($company) {
                        return $company->getTickerSymbol();
                    }, $dbResults);

                    $apiResults = array_filter($apiResults, function($result) use ($existingSymbols) {
                        return !in_array($result['symbol'], $existingSymbols);
                    });

                } catch (\Exception $e) {
                    // Log error but continue with DB results
                    $this->addFlash('warning', 'Could not fetch additional results from external sources');
                }
            }
        }

        return $this->render('company/search.html.twig', [
            'form' => $form->createView(),
            'dbResults' => $dbResults,
            'apiResults' => $apiResults,
            'searchTerm' => $searchTerm,
        ]);
    }

    #[Route('/import/{symbol}', name: 'company_import', methods: ['POST'])]
    public function importFromApi(string $symbol, StockDataService $stockDataService): Response
    {
        try {
            $company = $stockDataService->importCompany($symbol);
            $this->addFlash('success', 'Company successfully imported: ' . $company->getName());

            return $this->redirectToRoute('company_show', ['id' => $company->getId()]);
        } catch (\Exception $e) {
            $this->addFlash('error', 'Error importing company: ' . $e->getMessage());

            return $this->redirectToRoute('company_search');
        }
    }

    #[Route('/new', name: 'company_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager, NeuronAiService $neuronAiService): Response
    {
        $company = new Company();
        $form = $this->createForm(CompanyType::class, $company);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $companyName = $company->getName();

            // Auto-generate company details using Neuron AI
            try {
                if ($request->request->get('use_ai') === 'yes') {
                    $companyInfo = $neuronAiService->generateCompanyInfo($companyName);

                    if (!isset($companyInfo['error'])) {
                        $company->setIndustry($companyInfo['industry'] ?? $company->getIndustry());
                        $company->setSector($companyInfo['sector'] ?? $company->getSector());
                        $company->setHeadquarters($companyInfo['headquarters'] ?? $company->getHeadquarters());
                        $company->setDescription($companyInfo['description'] ?? $company->getDescription());
                    }
                }
            } catch (\Exception $e) {
                $this->addFlash('warning', 'AI enhancement failed, but company was created with provided information.');
            }

            $company->setUpdatedAt(new \DateTimeImmutable());
            $entityManager->persist($company);
            $entityManager->flush();

            $this->addFlash('success', 'Company created successfully.');

            return $this->redirectToRoute('company_show', ['id' => $company->getId()]);
        }

        return $this->render('company/new.html.twig', [
            'company' => $company,
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}', name: 'company_show', methods: ['GET'])]
    public function show(Company $company): Response
    {
        return $this->render('company/show.html.twig', [
            'company' => $company,
        ]);
    }

    #[Route('/{id}/edit', name: 'company_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, Company $company, EntityManagerInterface $entityManager): Response
    {
        $form = $this->createForm(CompanyType::class, $company);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $company->setUpdatedAt(new \DateTimeImmutable());
            $entityManager->flush();

            $this->addFlash('success', 'Company updated successfully.');

            return $this->redirectToRoute('company_show', ['id' => $company->getId()]);
        }

        return $this->render('company/edit.html.twig', [
            'company' => $company,
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}/financial', name: 'company_financial', methods: ['GET'])]
    public function financial(Company $company): Response
    {
        return $this->render('company/financial.html.twig', [
            'company' => $company,
            'financialData' => $company->getFinancialData(),
        ]);
    }

    #[Route('/{id}/news', name: 'company_news', methods: ['GET'])]
    public function news(Company $company, Request $request, StockDataService $stockDataService): Response
    {
        $limit = $request->query->getInt('limit', 10);
        $days = $request->query->getInt('days', 30);

        // Get news from the service
        $companyNews = $stockDataService->getCompanyNews($company->getTickerSymbol(), $limit);

        // Get business headlines for comparison/context
        $marketNews = [];
        try {
            $newsApiClient = $this->getParameter('news_api.api_key')
                ? $this->container->get('App\Service\ApiClient\NewsApiClient')
                : null;

            if ($newsApiClient) {
                $marketNews = $newsApiClient->getTopHeadlines('business', 'us', 5);
            }
        } catch (\Exception $e) {
            $this->addFlash('warning', 'Could not retrieve market headlines: ' . $e->getMessage());
        }

        return $this->render('company/news.html.twig', [
            'company' => $company,
            'news' => $companyNews,
            'marketNews' => $marketNews,
            'limit' => $limit,
            'days' => $days,
        ]);
    }

    #[Route('/{id}/analyst-ratings', name: 'company_analyst_ratings', methods: ['GET'])]
    public function analystRatings(Company $company, Request $request, StockDataService $stockDataService): Response
    {
        // Get analyst ratings data
        $ratingsData = $stockDataService->getAnalystRatings($company->getTickerSymbol());

        // Get consensus data
        $consensusData = $stockDataService->getAnalystConsensus($company->getTickerSymbol());

        // Get current stock price for context
        $quote = $stockDataService->getStockQuote($company->getTickerSymbol());

        return $this->render('company/analyst_ratings.html.twig', [
            'company' => $company,
            'ratings' => $ratingsData['ratings'] ?? [],
            'consensus' => $consensusData,
            'currentPrice' => $quote['price'] ?? 0,
        ]);
    }

    #[Route('/{id}/insider-trading', name: 'company_insider_trading', methods: ['GET'])]
    public function insiderTrading(Company $company, Request $request, StockDataService $stockDataService): Response
    {
        $limit = $request->query->getInt('limit', 20);

        // Get insider trading data
        $insiderData = $stockDataService->getInsiderTrading($company->getTickerSymbol(), $limit);

        // Get current stock quote for context
        $quote = $stockDataService->getStockQuote($company->getTickerSymbol());

        return $this->render('company/insider_trading.html.twig', [
            'company' => $company,
            'insiderData' => $insiderData,
            'currentPrice' => $quote['price'] ?? 0,
            'limit' => $limit
        ]);
    }

    #[Route('/{id}/institutional-ownership', name: 'company_institutional_ownership', methods: ['GET'])]
    public function institutionalOwnership(Company $company, Request $request, StockDataService $stockDataService): Response
    {
        $limit = $request->query->getInt('limit', 20);

        // Get institutional ownership data
        $ownershipData = $stockDataService->getInstitutionalOwnership($company->getTickerSymbol(), $limit);

        // Get current stock quote and total shares outstanding (if available)
        $quote = $stockDataService->getStockQuote($company->getTickerSymbol());
        $sharesOutstanding = $quote['sharesOutstanding'] ?? 0;

        // Calculate total institutional ownership percentage
        $totalShares = 0;
        foreach ($ownershipData as $institution) {
            $totalShares += $institution['sharesHeld'] ?? 0;
        }

        $institutionalOwnershipPercent = 0;
        if ($sharesOutstanding > 0) {
            $institutionalOwnershipPercent = ($totalShares / $sharesOutstanding) * 100;
        }

        return $this->render('company/institutional_ownership.html.twig', [
            'company' => $company,
            'ownershipData' => $ownershipData,
            'institutionalOwnershipPercent' => $institutionalOwnershipPercent,
            'sharesOutstanding' => $sharesOutstanding,
            'limit' => $limit
        ]);
    }

    #[Route('/{id}/stockprices', name: 'company_stockprices', methods: ['GET'])]
    public function stockPrices(
        Company $company,
        Request $request,
        StockDataService $stockDataService
    ): Response {
        $interval = $request->query->get('interval', 'daily');
        $period = $request->query->get('period', '3m');

        // Map period to limit
        $limit = match($period) {
            '1m' => 30,
            '3m' => 90,
            '6m' => 180,
            '1y' => 365,
            '5y' => 1825,
            default => 90,
        };

        // Get repository for filtering and specialized queries
        $repository = $this->getDoctrine()->getRepository(\App\Entity\StockPrice::class);

        // Check if we need to import data
        $latestPrice = $repository->findMostRecent($company, $interval);
        $now = new \DateTime();
        $needsUpdate = false;

        if (!$latestPrice) {
            $needsUpdate = true;
        } else {
            $daysDiff = $now->diff($latestPrice->getDate())->days;

            // If data is older than 1 day for daily, 7 days for weekly, 30 days for monthly, update
            $updateThreshold = match($interval) {
                'daily' => 1,
                'weekly' => 7,
                'monthly' => 30,
                default => 1,
            };

            if ($daysDiff > $updateThreshold) {
                $needsUpdate = true;
            }
        }

        // Import new data if needed
        if ($needsUpdate) {
            $importLimit = match($interval) {
                'daily' => 365, // 1 year of daily data
                'weekly' => 260, // 5 years of weekly data
                'monthly' => 120, // 10 years of monthly data
                default => 365,
            };

            $stockDataService->importHistoricalPrices($company, $interval, $importLimit);
        }

        // Get date range based on selected period
        $endDate = new \DateTime();
        $startDate = clone $endDate;

        switch ($period) {
            case '1m':
                $startDate->modify('-1 month');
                break;
            case '3m':
                $startDate->modify('-3 months');
                break;
            case '6m':
                $startDate->modify('-6 months');
                break;
            case '1y':
                $startDate->modify('-1 year');
                break;
            case '5y':
                $startDate->modify('-5 years');
                break;
            default:
                $startDate->modify('-3 months');
        }

        // Get prices for the selected date range
        $prices = $repository->findByDateRange($company, $startDate, $endDate, $interval);

        // Prepare data for charts
        $chartData = [
            'dates' => [],
            'prices' => [
                'close' => [],
                'open' => [],
                'high' => [],
                'low' => [],
            ],
            'volumes' => [],
            'changes' => [],
        ];

        // Reverse to get chronological order
        $prices = array_reverse($prices);

        foreach ($prices as $price) {
            $chartData['dates'][] = $price->getDate()->format('Y-m-d');
            $chartData['prices']['close'][] = $price->getClose();
            $chartData['prices']['open'][] = $price->getOpen();
            $chartData['prices']['high'][] = $price->getHigh();
            $chartData['prices']['low'][] = $price->getLow();
            $chartData['volumes'][] = $price->getVolume();
            $chartData['changes'][] = $price->getChangePercent();
        }

        // Calculate additional metrics based on the data
        $metrics = [];

        if (!empty($prices)) {
            // Latest price data
            $latestPrice = $prices[count($prices) - 1];
            $metrics['latestPrice'] = $latestPrice->getClose();
            $metrics['latestChange'] = $latestPrice->getChange();
            $metrics['latestChangePercent'] = $latestPrice->getChangePercent();
            $metrics['latestDate'] = $latestPrice->getDate()->format('Y-m-d');

            // Historical price range
            $highValues = array_map(function($price) { return $price->getHigh(); }, $prices);
            $lowValues = array_map(function($price) { return $price->getLow(); }, $prices);
            $metrics['periodHigh'] = max($highValues);
            $metrics['periodLow'] = min($lowValues);

            // 50-day and 200-day moving averages (if daily data)
            if ($interval === 'daily' && count($prices) >= 50) {
                $last50Prices = array_slice($prices, -50);
                $ma50Sum = 0;
                foreach ($last50Prices as $price) {
                    $ma50Sum += $price->getClose();
                }
                $metrics['ma50'] = $ma50Sum / 50;

                if (count($prices) >= 200) {
                    $last200Prices = array_slice($prices, -200);
                    $ma200Sum = 0;
                    foreach ($last200Prices as $price) {
                        $ma200Sum += $price->getClose();
                    }
                    $metrics['ma200'] = $ma200Sum / 200;
                }
            }

            // Calculate average daily volume
            $volumes = array_map(function($price) { return $price->getVolume(); }, $prices);
            $metrics['avgVolume'] = array_sum($volumes) / count($volumes);
        }

        return $this->render('company/stock_prices.html.twig', [
            'company' => $company,
            'chartData' => $chartData,
            'metrics' => $metrics,
            'prices' => $prices,
            'interval' => $interval,
            'period' => $period,
        ]);
    }

    #[Route('/{id}/generate-financial', name: 'company_generate_financial', methods: ['POST'])]
    public function generateFinancial(
        Request $request,
        Company $company,
        NeuronAiService $neuronAiService,
        EntityManagerInterface $entityManager
    ): Response {
        // Only allow XHR requests
        if (!$request->isXmlHttpRequest()) {
            return $this->json(['success' => false, 'message' => 'AJAX requests only'], 400);
        }

        try {
            // Get financial data from AI
            $reportData = $neuronAiService->generateFinancialAnalysis($company->getName(), '10-K');

            if (isset($reportData['error'])) {
                return $this->json(['success' => false, 'message' => $reportData['error']], 500);
            }

            // Create quarterly data entries (4 quarters worth)
            $currentYear = (int)date('Y');
            $quarters = ['Q1', 'Q2', 'Q3', 'Q4'];

            // Use the revenue value from the API and distribute it across quarters with some variation
            $totalRevenue = $reportData['revenue'] ?? mt_rand(500000000, 5000000000);
            $totalNetIncome = $reportData['netIncome'] ?? ($totalRevenue * mt_rand(5, 25) / 100);

            foreach ($quarters as $i => $quarter) {
                // Create a new financial data entry
                $financialData = new \App\Entity\FinancialData();
                $financialData->setCompany($company);
                $financialData->setFiscalQuarter($quarter);
                $financialData->setFiscalYear($currentYear - 1);

                // Set financial values with some variation between quarters
                $quarterRevenue = $totalRevenue * (0.2 + (mt_rand(-5, 10) / 100));
                $financialData->setRevenue($quarterRevenue);

                $quarterIncome = $totalNetIncome * (0.2 + (mt_rand(-10, 15) / 100));
                $financialData->setNetIncome($quarterIncome);

                // Set other financial fields with realistic values
                $financialData->setEps($quarterIncome / mt_rand(80000000, 120000000));
                $financialData->setEbitda($quarterRevenue * mt_rand(15, 35) / 100);
                $financialData->setProfitMargin($quarterIncome / $quarterRevenue);

                // Generate other key metrics
                $financialData->setPeRatio(mt_rand(12, 30) + (mt_rand(0, 100) / 100));
                $financialData->setDividendYield(mt_rand(0, 5) / 100);
                $financialData->setRoe(mt_rand(5, 25) / 100);
                $financialData->setDebtToEquity(mt_rand(5, 30) / 10);
                $financialData->setCurrentRatio(mt_rand(10, 30) / 10);

                // Balance sheet items
                $totalAssets = $quarterRevenue * (mt_rand(150, 300) / 100);
                $financialData->setTotalAssets($totalAssets);
                $financialData->setTotalLiabilities($totalAssets * (mt_rand(40, 70) / 100));
                $financialData->setShareholderEquity($totalAssets - $financialData->getTotalLiabilities());
                $financialData->setCashAndEquivalents($totalAssets * (mt_rand(5, 20) / 100));
                $financialData->setLongTermDebt($financialData->getTotalLiabilities() * (mt_rand(30, 70) / 100));

                // Set gross and operating margins
                $financialData->setGrossMargin($quarterRevenue * (mt_rand(35, 75) / 100) / $quarterRevenue);
                $financialData->setOperatingMargin($quarterRevenue * (mt_rand(15, 40) / 100) / $quarterRevenue);

                // Set market cap
                $financialData->setMarketCap($totalAssets * (mt_rand(80, 150) / 100));

                // Persist to database
                $entityManager->persist($financialData);
            }

            $entityManager->flush();

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

    #[Route('/{id}/leadership', name: 'company_leadership', methods: ['GET'])]
    public function leadership(Company $company, LinkedInService $linkedInService = null): Response
    {
        // If LinkedInService is available, check for LinkedIn connection data
        $linkedInEnabled = false;
        if ($linkedInService !== null) {
            $linkedInEnabled = true;
        }

        return $this->render('company/leadership.html.twig', [
            'company' => $company,
            'executives' => $company->getExecutiveProfiles(),
            'linkedInEnabled' => $linkedInEnabled
        ]);
    }

    #[Route('/{id}/generate-leadership', name: 'company_generate_leadership', methods: ['POST'])]
    public function generateLeadership(
        Request $request,
        Company $company,
        NeuronAiService $neuronAiService,
        EntityManagerInterface $entityManager
    ): Response {
        // Only allow XHR requests
        if (!$request->isXmlHttpRequest()) {
            return $this->json(['success' => false, 'message' => 'AJAX requests only'], 400);
        }

        try {
            // Generate executive profiles
            $executives = [
                ['name' => 'John Smith', 'title' => 'Chief Executive Officer'],
                ['name' => 'Sarah Johnson', 'title' => 'Chief Financial Officer'],
                ['name' => 'David Chen', 'title' => 'Chief Technology Officer'],
                ['name' => 'Emily Rodriguez', 'title' => 'Chief Marketing Officer']
            ];

            foreach ($executives as $executiveData) {
                // Create a new executive profile
                $executive = new \App\Entity\ExecutiveProfile();
                $executive->setCompany($company);
                $executive->setName($executiveData['name']);
                $executive->setTitle($executiveData['title']);

                // Try to get AI-generated profile
                try {
                    $aiData = $neuronAiService->generateExecutiveProfile(
                        $executiveData['name'],
                        $company->getName(),
                        $executiveData['title']
                    );

                    if (!isset($aiData['error'])) {
                        // Use the AI data
                        $executive->setBio($aiData['biography'] ?? 'Executive biography would appear here.');
                        $executive->setEducation($aiData['education'] ?? 'MBA, Harvard Business School');
                        $executive->setPreviousCompanies($aiData['previousCompanies'] ?? 'Previous Company, Inc.');
                        $executive->setAchievements($aiData['achievements'] ?? 'Notable industry accomplishments and recognition.');
                    } else {
                        // Use fallback data
                        $executive->setBio('A seasoned executive with years of experience in the industry.');

                        if ($executiveData['title'] === 'Chief Executive Officer') {
                            $executive->setEducation('MBA, Stanford University');
                            $executive->setPreviousCompanies('Fortune 500 Company, Tech Innovators Inc.');
                            $executive->setAchievements('Led company to record growth, Industry leadership award recipient');
                        } elseif ($executiveData['title'] === 'Chief Financial Officer') {
                            $executive->setEducation('MBA, Wharton School of Business');
                            $executive->setPreviousCompanies('Global Finance Corp, Investment Bank LLC');
                            $executive->setAchievements('Structured major acquisitions, Improved operational efficiency');
                        } elseif ($executiveData['title'] === 'Chief Technology Officer') {
                            $executive->setEducation('PhD in Computer Science, MIT');
                            $executive->setPreviousCompanies('Tech Giants Inc, Startup Innovations');
                            $executive->setAchievements('Multiple patents, Led major platform redesign');
                        } else {
                            $executive->setEducation('MBA, Northwestern University');
                            $executive->setPreviousCompanies('Global Brands Inc, Marketing Experts LLC');
                            $executive->setAchievements('Award-winning campaigns, Digital transformation leader');
                        }
                    }
                } catch (\Exception $e) {
                    // Use fallback data
                    $executive->setBio('A seasoned executive with years of experience in the industry.');
                    $executive->setEducation('MBA, Leading University');
                    $executive->setPreviousCompanies('Previous notable companies');
                    $executive->setAchievements('Industry achievements and recognition');
                }

                // Set other executive fields
                $executive->setStartYear(mt_rand(2010, 2020));
                $executive->setPhotoUrl(null); // No photo available

                // Persist to database
                $entityManager->persist($executive);
            }

            $entityManager->flush();

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

    #[Route('/{id}/competitors', name: 'company_competitors', methods: ['GET'])]
    public function competitors(Company $company): Response
    {
        return $this->render('company/competitors.html.twig', [
            'company' => $company,
            'competitorAnalyses' => $company->getCompetitorAnalyses(),
        ]);
    }

    #[Route('/{id}/generate-competitors', name: 'company_generate_competitors', methods: ['POST'])]
    public function generateCompetitors(
        Request $request,
        Company $company,
        NeuronAiService $neuronAiService,
        EntityManagerInterface $entityManager
    ): Response {
        // Only allow XHR requests
        if (!$request->isXmlHttpRequest()) {
            return $this->json(['success' => false, 'message' => 'AJAX requests only'], 400);
        }

        try {
            // Generate competitor data using AI
            $competitors = [
                ['name' => 'Competitor A', 'share' => 20],
                ['name' => 'Competitor B', 'share' => 15],
                ['name' => 'Competitor C', 'share' => 10],
                ['name' => 'Others', 'share' => 10]
            ];

            // Company's market share
            $companyMarketShare = 45;

            // Create a competitor analysis for the company
            $competitorAnalysis = new \App\Entity\CompetitorAnalysis();
            $competitorAnalysis->setCompany($company);
            $competitorAnalysis->setCompanyMarketShare($companyMarketShare);

            // Get some competitor insights from the AI service
            $aiData = $neuronAiService->generateCompetitorAnalysis($company->getName(), $competitors[0]['name']);

            if (isset($aiData['error'])) {
                return $this->json(['success' => false, 'message' => $aiData['error']], 500);
            }

            // Set analysis data
            $competitorAnalysis->setIndustryOverview($aiData['overview'] ?? 'Industry overview text would appear here, describing key trends and factors affecting all participants.');
            $competitorAnalysis->setMarketPosition($aiData['strategicInitiatives'] ?? 'Analysis of market position relative to competitors, including unique advantages and challenges.');

            // Set SWOT analysis
            $competitorAnalysis->setSwotStrengths("Strong brand recognition\nInnovative product portfolio\nEfficient supply chain\nStrong financial position\nGlobal market presence");
            $competitorAnalysis->setSwotWeaknesses("High production costs\nFocus on mature markets\nLimited product diversification\nDependence on specific suppliers\nRegulatory compliance challenges");
            $competitorAnalysis->setSwotOpportunities("Emerging market expansion\nNew product development\nStrategic acquisitions\nGrowing demand for eco-friendly solutions\nE-commerce channel growth");
            $competitorAnalysis->setSwotThreats("Intense competition\nChanging consumer preferences\nRegulatory changes\nSupply chain disruptions\nEconomic downturn");

            // Store competitor details
            foreach ($competitors as $competitor) {
                $competitorDetail = new \App\Entity\CompetitorDetail();
                $competitorDetail->setCompetitorAnalysis($competitorAnalysis);
                $competitorDetail->setName($competitor['name']);
                $competitorDetail->setMarketShare($competitor['share']);
                $entityManager->persist($competitorDetail);
            }

            // Persist the analysis
            $entityManager->persist($competitorAnalysis);
            $entityManager->flush();

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