<?php

namespace App\Services;

use App\Models\PlatformSetting;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class LandingPageContentService
{
    public const SETTING_KEY = 'landing_page_content';

    public function content(): array
    {
        $stored = PlatformSetting::get(self::SETTING_KEY);
        $decoded = is_string($stored) ? json_decode($stored, true) : null;

        return array_replace_recursive($this->defaults(), is_array($decoded) ? $decoded : []);
    }

    public function save(array $content): array
    {
        $content = array_replace_recursive($this->defaults(), $content);

        PlatformSetting::set(
            self::SETTING_KEY,
            json_encode($content, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
            'landing_page'
        );

        return $content;
    }

    public function storeImage(UploadedFile $file, string $slot): string
    {
        $directory = public_path('images/landing/uploads');
        File::ensureDirectoryExists($directory);

        $extension = strtolower($file->extension() ?: 'jpg');
        $filename = Str::slug($slot).'-'.Str::uuid().'.'.$extension;
        $file->move($directory, $filename);

        return '/images/landing/uploads/'.$filename;
    }

    public function defaults(): array
    {
        return [
            'meta' => [
                'title' => 'BrandX ERP | Smart ERP for UAE Typing Centers',
                'description' => 'Run customers, applications, documents, payments, renewals and staff operations from one secure UAE typing-center ERP.',
            ],
            'theme' => [
                'primary' => '#ff5a0a',
                'navy' => '#071c31',
                'accent' => '#ff8a3d',
            ],
            'brand' => [
                'name' => 'BrandX',
                'descriptor' => 'UAE TYPING CENTER ERP',
                'logo_path' => null,
            ],
            'nav' => [
                'features' => 'Features',
                'workflow' => 'Workflow',
                'advantages' => 'Why BrandX ERP',
                'contact' => 'Contact',
                'login' => 'Sign in',
            ],
            'hero' => [
                'eyebrow' => 'AI-POWERED OPERATIONS FOR THE UAE',
                'title' => 'Smart ERP for',
                'highlight' => 'UAE Typing Centers',
                'description' => 'Manage customers, documents, government applications, payments, renewals and every member of your team from one powerful workspace.',
                'primary_label' => 'Request a Demo',
                'primary_url' => '#contact',
                'secondary_label' => 'Explore BrandX ERP',
                'secondary_url' => '#features',
                'image_eyebrow' => 'LIVE OPERATIONS',
                'image_title' => 'Every customer. Every stage. One view.',
                'image_metric' => '360°',
                'image_metric_label' => 'operational clarity',
                'status_title' => 'Application on track',
                'status_description' => 'Updates stay visible to the whole team',
                'highlights' => [
                    ['label' => 'All-in-one solution'],
                    ['label' => 'Built for UAE workflows'],
                    ['label' => 'Secure by design'],
                    ['label' => 'Expert support'],
                ],
            ],
            'partners' => [
                'heading' => 'Built for the UAE government-services ecosystem',
                'items' => [
                    ['name' => 'AMER Services'],
                    ['name' => 'Tasheel Centers'],
                    ['name' => 'Dubai Economy'],
                    ['name' => 'ICP Services'],
                    ['name' => 'MOHRE'],
                    ['name' => 'Dubai Courts'],
                ],
            ],
            'core' => [
                'eyebrow' => 'COMPLETE TYPING CENTER MANAGEMENT',
                'title' => 'Everything your typing center needs in one system',
                'description' => 'One connected platform replaces scattered tools and gives every team a clear view of the work.',
                'items' => [
                    ['icon' => 'users', 'title' => 'Customer Management', 'description' => 'Keep customer, family and corporate profiles organized in one place.'],
                    ['icon' => 'document', 'title' => 'Document Vault', 'description' => 'Collect, verify and retrieve documents securely in seconds.'],
                    ['icon' => 'route', 'title' => 'Application Tracking', 'description' => 'See every application stage and ownership change in real time.'],
                    ['icon' => 'wallet', 'title' => 'Payments & Billing', 'description' => 'Handle invoices, VAT, receipts, cash sessions and refunds accurately.'],
                    ['icon' => 'bell', 'title' => 'Renewal Reminders', 'description' => 'Turn document expiries into timely follow-ups and repeat business.'],
                    ['icon' => 'chart', 'title' => 'Reports & Analytics', 'description' => 'Understand workload, revenue, collections and team performance.'],
                ],
            ],
            'workflow' => [
                'eyebrow' => 'STREAMLINE EVERY STEP',
                'title' => 'From customer entry to application completion',
                'description' => 'BrandX ERP gives your team a consistent workflow so customers move faster with fewer mistakes and complete visibility.',
                'image_eyebrow' => 'ONE WORKFLOW',
                'image_title' => 'From intake to delivery',
                'bullets' => [
                    ['text' => 'Customer search and service selection'],
                    ['text' => 'Document checklist and verification'],
                    ['text' => 'Automatic fee calculation and payment'],
                    ['text' => 'Typist processing and government submission'],
                    ['text' => 'Approval, return and delivery tracking'],
                    ['text' => 'Renewal monitoring and follow-up'],
                ],
            ],
            'features' => [
                'eyebrow' => 'POWERFUL FEATURES',
                'title' => 'Built for faster typing-center operations',
                'description' => 'Practical tools for front desk, typists, PROs, cashiers, accountants, managers and customers.',
                'items' => [
                    ['icon' => 'queue', 'title' => 'Typist Work Queue', 'description' => 'Assign, claim and track work by priority and status.'],
                    ['icon' => 'checklist', 'title' => 'Smart Checklists', 'description' => 'Make sure every required document is collected.'],
                    ['icon' => 'message', 'title' => 'Customer Updates', 'description' => 'Keep customers informed through every milestone.'],
                    ['icon' => 'building', 'title' => 'Corporate Management', 'description' => 'Manage companies, contacts and employee transactions.'],
                    ['icon' => 'branch', 'title' => 'Branch & Counter Control', 'description' => 'Operate multiple branches, departments and counters.'],
                    ['icon' => 'shield-users', 'title' => 'Roles & Permissions', 'description' => 'Give each team member the right access and data scope.'],
                    ['icon' => 'cash', 'title' => 'Daily Cash Closing', 'description' => 'Close counters and reconcile collections with confidence.'],
                    ['icon' => 'portal', 'title' => 'Customer Portal', 'description' => 'Let customers follow applications and documents online.'],
                    ['icon' => 'briefcase', 'title' => 'Corporate Portal', 'description' => 'Give business clients a clear view of employee services.'],
                    ['icon' => 'language', 'title' => 'English & Arabic', 'description' => 'Support multilingual teams and UAE service operations.'],
                    ['icon' => 'shield', 'title' => 'Secure Data Access', 'description' => 'Protect sensitive records with scoped, auditable access.'],
                    ['icon' => 'mobile', 'title' => 'Mobile Friendly', 'description' => 'Work securely from the counter, office or on the move.'],
                ],
            ],
            'advantages' => [
                'eyebrow' => 'WHY TYPING CENTERS CHOOSE BRANDX ERP',
                'title' => 'More efficiency. More customers. More profit.',
                'description' => 'Reduce manual work, prevent costly mistakes and create a service experience that keeps customers coming back.',
                'image_title' => 'Less admin. More service.',
                'image_description' => 'Give your team time back for the work that matters.',
                'bullets' => [
                    ['text' => 'Reduce repetitive manual work and save time'],
                    ['text' => 'Improve accuracy with guided workflows'],
                    ['text' => 'Never miss an expiry or renewal opportunity'],
                    ['text' => 'Increase customer confidence and satisfaction'],
                    ['text' => 'Make better decisions with live business data'],
                ],
            ],
            'cta' => [
                'eyebrow' => 'READY TO MODERNIZE YOUR TYPING CENTER?',
                'title' => 'Run your typing center smarter with BrandX ERP',
                'description' => 'See how one connected workspace can simplify your operation from the first customer visit to the next renewal.',
                'button_label' => 'Request a Demo',
                'button_url' => 'mailto:sales@govix.ae',
                'phone_label' => 'Call us',
                'phone' => '+971 50 123 4567',
                'points' => [
                    ['title' => 'One connected ERP', 'description' => 'Everything your operation needs.'],
                    ['title' => 'Trusted UAE workflow', 'description' => 'Designed around local service teams.'],
                    ['title' => 'Expert onboarding', 'description' => 'Practical support from day one.'],
                ],
            ],
            'footer' => [
                'description' => 'AI-powered operations for modern UAE typing centers and government-services teams.',
                'copyright' => '© 2026 BrandX ERP. All rights reserved.',
                'privacy_label' => 'Privacy Policy',
                'privacy_url' => '#',
                'terms_label' => 'Terms & Conditions',
                'terms_url' => '#',
                'email' => 'hello@govix.ae',
                'instagram_url' => null,
                'instagram_label' => 'Instagram',
                'linkedin_url' => null,
                'linkedin_label' => 'LinkedIn',
            ],
            'media' => [
                'hero' => '/images/landing/govix-hero.png',
                'workflow' => '/images/landing/govix-workflow.png',
                'advantages' => '/images/landing/govix-hero.png',
            ],
        ];
    }
}
