<?php

namespace App\Controller\Admin;

use App\Entity\Tenant\ThemeSetting;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ThemeController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {}

    #[Route('/theme', name: 'admin_theme', methods: ['GET', 'POST'])]
    public function index(Request $request): Response
    {
        if ($request->isMethod('POST')) {
            if (!$this->isCsrfTokenValid('theme_settings', $request->request->get('_token'))) {
                $this->addFlash('error', 'Geçersiz CSRF token. Lütfen tekrar deneyin.');

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

            $repo = $this->entityManager->getRepository(ThemeSetting::class);
            $submittedSettings = $request->request->all('settings');

            // Define all theme settings with their groups
            $themeFields = [
                // Genel (General)
                'site_logo' => 'genel',
                'site_favicon' => 'genel',
                'site_name' => 'genel',
                'site_slogan' => 'genel',
                'footer_text' => 'genel',
                // Renkler (Colors)
                'primary_color' => 'renkler',
                'secondary_color' => 'renkler',
                'header_bg' => 'renkler',
                'footer_bg' => 'renkler',
                'accent_color' => 'renkler',
                // Tipografi (Typography)
                'body_font' => 'tipografi',
                'heading_font' => 'tipografi',
                'font_size_base' => 'tipografi',
                // Layout
                'sidebar_position' => 'layout',
                'header_style' => 'layout',
                'footer_columns' => 'layout',
                'show_breadcrumb' => 'layout',
                // Sosyal (Social)
                'facebook_url' => 'sosyal',
                'twitter_url' => 'sosyal',
                'instagram_url' => 'sosyal',
                'youtube_url' => 'sosyal',
                'telegram_url' => 'sosyal',
            ];

            $booleanFields = ['show_breadcrumb'];

            foreach ($themeFields as $key => $group) {
                $value = $submittedSettings[$key] ?? null;

                if (in_array($key, $booleanFields, true)) {
                    $value = $value ? '1' : '0';
                }

                $setting = $repo->findOneBy(['themeKey' => $key]);
                if (!$setting) {
                    $setting = new ThemeSetting();
                    $setting->setThemeKey($key);
                    $setting->setThemeGroup($group);
                    $this->entityManager->persist($setting);
                }

                $setting->setThemeValue($value);
            }

            $this->entityManager->flush();

            $this->addFlash('success', 'Tema ayarları başarıyla kaydedildi.');

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

        // GET: Load all settings grouped by themeGroup
        $allSettings = $this->entityManager->getRepository(ThemeSetting::class)->findAll();

        $settingsMap = [];
        foreach ($allSettings as $setting) {
            $settingsMap[$setting->getThemeKey()] = $setting->getThemeValue();
        }

        return $this->render('admin/theme/index.html.twig', [
            'settings' => $settingsMap,
        ]);
    }
}
