<?php

namespace App\Services\System\Setting;

use App\Models\HomepageConfiguration;
use App\Services\Concerns\CachesQuery;
use App\Services\Concerns\RunsInTransaction;
use App\Services\Concerns\SyncsPhotos;
use App\Services\Media\MediaService;
use App\Support\Media\MediaPresenter;

class HomepageSettingService
{
    use CachesQuery, RunsInTransaction, SyncsPhotos;

    public function __construct(
        private readonly MediaService $mediaService,
    ) {}

    public function homepageData(): array
    {
        return $this->cacheRemember('homepage:settings', 86400, function () {
            $configuration = HomepageConfiguration::instance();
            $configuration->load('media');

            $heroImage = MediaPresenter::first($configuration, 'hero_image');
            $aboutImage = MediaPresenter::first($configuration, 'about_image');
            $galleryImages = MediaPresenter::collection($configuration, 'gallery');

            return [
                'hero_image_url' => $heroImage['url'] ?? null,
                'about_image_url' => $aboutImage['url'] ?? null,
                'gallery_images' => $galleryImages,
            ];
        });
    }

    public function updateHomepage(array $validated): void
    {
        $this->runInTransaction(
            function () use ($validated): void {
                $configuration = HomepageConfiguration::instance();

                $this->syncPhotos(
                    $configuration,
                    [
                        'photos' => isset($validated['hero_image']) ? [$validated['hero_image']] : null,
                        's3_keys' => isset($validated['hero_image_s3_key']) ? [$validated['hero_image_s3_key']] : null,
                    ],
                    maxPhotos: 1,
                    required: false,
                    collection: 'hero_image',
                );

                $this->syncPhotos(
                    $configuration,
                    [
                        'photos' => isset($validated['about_image']) ? [$validated['about_image']] : null,
                        's3_keys' => isset($validated['about_image_s3_key']) ? [$validated['about_image_s3_key']] : null,
                    ],
                    maxPhotos: 1,
                    required: false,
                    collection: 'about_image',
                );

                $this->syncPhotos(
                    $configuration,
                    [
                        'photos' => $validated['gallery_images'] ?? null,
                        'remove_media_ids' => $validated['gallery_images_remove'] ?? null,
                        's3_keys' => $validated['gallery_s3_keys'] ?? null,
                    ],
                    maxPhotos: 10,
                    required: false,
                    collection: 'gallery',
                );
            },
            'Gagal memperbarui pengaturan homepage',
        );

        $this->cacheForget('homepage:settings');
        $this->cacheForget('homepage:page_data');
    }
}
