<?php

use Botble\Base\Facades\Html;
use Botble\Portfolio\Models\Package;
use Botble\Portfolio\Models\Project;
use Botble\Portfolio\Models\Service;
use Botble\Portfolio\Models\ServiceCategory;
use Botble\Shortcode\Compilers\Shortcode as ShortcodeCompiler;
use Botble\Shortcode\Events\ShortcodeAdminConfigRendering;
use Botble\Shortcode\Facades\Shortcode;
use Botble\Theme\Facades\Theme;

app()->booted(function () {
    if (! is_plugin_active('portfolio')) {
        return;
    }

    Shortcode::register('featured-projects', __('Featured projects'), __('Featured projects'), function (ShortcodeCompiler $shortcode): string|null {
        if (! ($projectIds = Shortcode::fields()->getIds('project_ids', $shortcode))) {
            return null;
        }

        $projects = Project::query()
            ->whereIn('id', $projectIds)
            ->wherePublished()
            ->get();

        return Theme::partial('shortcodes.featured-projects.index', compact('shortcode', 'projects'));
    });

    Shortcode::setAdminConfig('featured-projects', function (array $attributes): string|null {
        $projects = Project::query()
            ->wherePublished()
            ->pluck('name', 'id')
            ->all();

        return Theme::partial('shortcodes.featured-projects.admin', compact('attributes', 'projects'));
    });

    Shortcode::register('featured-service-categories', __('Featured service categories'), __('Featured service categories'), function (ShortcodeCompiler $shortcode): string|null {
        if (! ($categoryIds = Shortcode::fields()->getIds('category_ids', $shortcode))) {
            return null;
        }

        $categories = ServiceCategory::query()
            ->whereIn('id', $categoryIds)
            ->wherePublished()
            ->get();

        return Theme::partial('shortcodes.featured-service-categories.index', compact('shortcode', 'categories'));
    });

    Shortcode::setAdminConfig('featured-service-categories', function (array $attributes): string|null {
        $categories = ServiceCategory::query()
            ->wherePublished()
            ->pluck('name', 'id')
            ->all();

        return Theme::partial('shortcodes.featured-service-categories.admin', compact('attributes', 'categories'));
    });

    Shortcode::register('pricing', __('Pricing'), __('Pricing'), function (ShortcodeCompiler $shortcode): string|null {
        if (! ($packageIds = Shortcode::fields()->getIds('package_ids', $shortcode))) {
            return null;
        }

        $packages = Package::query()
            ->wherePublished()
            ->whereIn('id', $packageIds)
            ->with(['metadata'])
            ->get();

        return Theme::partial('shortcodes.pricing.index', compact('shortcode', 'packages'));
    });

    Shortcode::setAdminConfig('pricing', function (array $attributes): string {
        $packages = Package::query()
            ->wherePublished()
            ->pluck('name', 'id')
            ->all();

        return
            Html::style('vendor/core/core/base/libraries/tagify/tagify.css') .
            Html::script('vendor/core/core/base/libraries/tagify/tagify.js') .
            Html::script('vendor/core/core/base/js/tags.js') .
            Theme::partial('shortcodes.pricing.admin', compact('attributes', 'packages'));
    });

    Shortcode::register('featured-services', __('Featured services'), __('Featured services'), function (ShortcodeCompiler $shortcode): string|null {
        if (! $serviceIds = Shortcode::fields()->getIds('service_ids', $shortcode)) {
            return null;
        }

        $services = Service::query()
            ->whereIn('id', $serviceIds)
            ->wherePublished()
            ->with(['metadata', 'slugable'])
            ->get();

        return Theme::partial('shortcodes.featured-services.index', compact('shortcode', 'services'));
    });

    Shortcode::setAdminConfig('featured-services', function (array $attributes): string|null {
        $services = Service::query()
            ->wherePublished()
            ->pluck('name', 'id')
            ->all();

        return Theme::partial('shortcodes.featured-services.admin', compact('attributes', 'services'));
    });

    Shortcode::register('services-list', __('Services list'), __('Services list'), function (ShortcodeCompiler $shortcode): string|null {
        if (! $serviceIds = Shortcode::fields()->getIds('service_ids', $shortcode)) {
            return null;
        }

        $services = Service::query()
            ->whereIn('id', $serviceIds)
            ->wherePublished()
            ->get();

        return Theme::partial('shortcodes.services-list.index', compact('shortcode', 'services'));
    });

    Shortcode::setAdminConfig('services-list', function (array $attributes): string|null {
        $services = Service::query()
            ->wherePublished()
            ->pluck('name', 'id')
            ->all();

        return Theme::partial('shortcodes.services-list.admin', compact('attributes', 'services'));
    });

    add_filter('request-quote-shortcode', function () {
        return Theme::getThemeNamespace('partials.shortcodes.request-quote.index');
    });

    app('events')->listen(ShortcodeAdminConfigRendering::class, function () {
        Shortcode::setAdminConfig('request-quote', function (array $attributes): string|null {
            return Theme::partial('shortcodes.request-quote.admin', compact('attributes'));
        });
    });
});
