<?php

namespace App\Services\Admin;

use App\Services\S3PresignedService;
use App\Settings\HomepageSettings;
use App\Settings\HRSettings;
use App\Settings\SocialMediaSettings;
use App\Settings\SystemSettings;

class AdminSettingsService
{
    public function __construct(
        private S3PresignedService $s3Service,
    ) {}

    public function getSystemData(): array
    {
        $settings = app(SystemSettings::class);

        return [
            'app_name' => $settings->app_name,
            'about_app' => $settings->about_app,
            'address' => $settings->address,
            'email' => $settings->email,
            'phone' => $settings->phone,
        ];
    }

    public function getHomepageData(): array
    {
        $settings = app(HomepageSettings::class);

        return [
            'hero_image_url' => $settings->hero_image_url ? $this->getTemporaryUrl($settings->hero_image_url) : null,
            'hero_image_key' => $settings->hero_image_url,
            'about_image_url' => $settings->about_image_url ? $this->getTemporaryUrl($settings->about_image_url) : null,
            'about_image_key' => $settings->about_image_url,
            'gallery_images' => array_map(
                fn ($key) => $this->getTemporaryUrl($key),
                $settings->gallery_images ?? [],
            ),
            'gallery_image_keys' => $settings->gallery_images ?? [],
        ];
    }

    public function getSocialMediaData(): array
    {
        $settings = app(SocialMediaSettings::class);

        return [
            'instagram_url' => $settings->instagram_url,
            'facebook_url' => $settings->facebook_url,
            'tiktok_url' => $settings->tiktok_url,
        ];
    }

    public function getHRData(): array
    {
        $settings = app(HRSettings::class);

        return [
            'scheduled_check_in_time' => $settings->scheduled_check_in_time,
            'scheduled_check_out_time' => $settings->scheduled_check_out_time,
            'late_penalty_amount' => $settings->late_penalty_amount,
            'absent_penalty_amount' => $settings->absent_penalty_amount,
        ];
    }

    public function updateSystem(array $data): void
    {
        $settings = app(SystemSettings::class);
        $settings->fill($data);
        $settings->save();
    }

    public function updateHomepage(array $data): void
    {
        $settings = app(HomepageSettings::class);

        $settings->hero_image_url = $data['hero_image_key'] ?? null;
        $settings->about_image_url = $data['about_image_key'] ?? null;
        $settings->gallery_images = $data['gallery_image_keys'] ?? [];

        $settings->save();
    }

    public function updateSocialMedia(array $data): void
    {
        $settings = app(SocialMediaSettings::class);
        $settings->fill($data);
        $settings->save();
    }

    public function updateHR(array $data): void
    {
        $settings = app(HRSettings::class);
        $settings->fill($data);
        $settings->save();
    }

    private function getTemporaryUrl(string $key): string
    {
        if (str_starts_with($key, 'http')) {
            return $key;
        }

        return $this->s3Service->getTemporaryUrl($key, 60);
    }
}
