<?php

namespace App\Controllers;

use App\Models\ArticleModel;
use App\Models\SettingModel;
use CodeIgniter\Exceptions\PageNotFoundException;

class BrandGuide extends BaseController
{
    public function index()
    {
        $articles = (new ArticleModel())
            ->where("is_published", 1)
            ->groupStart()
            ->where("published_at <=", date("Y-m-d H:i:s"))
            ->orWhere("published_at", null)
            ->groupEnd()
            ->orderBy("is_featured", "DESC")
            ->orderBy("published_at", "DESC")
            ->findAll(100);

        return view(
            "brand_guide/index",
            $this->viewData([
                "articles" => $articles,
                "featured" => $articles[0] ?? null,
            ]),
        );
    }

    public function show(string $slug)
    {
        $article = (new ArticleModel())
            ->where("slug", strtolower($slug))
            ->where("is_published", 1)
            ->groupStart()
            ->where("published_at <=", date("Y-m-d H:i:s"))
            ->orWhere("published_at", null)
            ->groupEnd()
            ->first();
        if (!$article) {
            throw PageNotFoundException::forPageNotFound();
        }

        $related = (new ArticleModel())
            ->where("is_published", 1)
            ->where("id !=", $article["id"])
            ->where("category", $article["category"])
            ->orderBy("published_at", "DESC")
            ->findAll(3);
        if (count($related) < 3) {
            $relatedIds = array_column($related, "id");
            $fallback = (new ArticleModel())
                ->where("is_published", 1)
                ->where("id !=", $article["id"]);
            if ($relatedIds) {
                $fallback->whereNotIn("id", $relatedIds);
            }
            $related = array_merge(
                $related,
                $fallback
                    ->orderBy("is_featured", "DESC")
                    ->orderBy("published_at", "DESC")
                    ->findAll(3 - count($related)),
            );
        }

        return view(
            "brand_guide/show",
            $this->viewData([
                "article" => $article,
                "relatedArticles" => $related,
            ]),
        );
    }

    private function viewData(array $data): array
    {
        $settings = (new SettingModel())->values([
            "call_phone_display" => "+91 98765 43210",
            "call_phone_href" => "+919876543210",
            "whatsapp_number" => "919876543210",
            "social_instagram" => "",
            "social_behance" => "",
            "social_facebook" => "",
            "social_linkedin" => "",
            "social_youtube" => "",
            "social_pinterest" => "",
        ]);

        return array_merge($data, [
            "phoneDisplay" => $settings["call_phone_display"],
            "phoneHref" => $settings["call_phone_href"],
            "whatsappNumber" => $settings["whatsapp_number"],
            "socialLinks" => $this->socialLinks($settings),
        ]);
    }

    private function socialLinks(array $settings): array
    {
        $links = [];
        foreach (
            [
                "Instagram" => "social_instagram",
                "Behance" => "social_behance",
                "Facebook" => "social_facebook",
                "LinkedIn" => "social_linkedin",
                "YouTube" => "social_youtube",
                "Pinterest" => "social_pinterest",
            ]
            as $label => $key
        ) {
            if (!empty($settings[$key])) {
                $links[$label] = $settings[$key];
            }
        }
        return $links;
    }
}
