<?php

namespace Opentalent\OtCore\Tests\Unit\Fixtures;

class WebsiteFixtures
{
    private array $websites = [
        [ 'uid' => 1, 'organization_id' => 1, 'subdomain' => 'em1', 'custom_domain' => '', 'locale' => 'fr_FR', 'matomo_site_id' => null, 'template' => 'Classic', 'template_preferences' => '{"themeColor":"light-blue","displayCarousel":"1"}', 'organization_name' => 'Ecole de musique 1', 'deleted' => 0  ],
        [ 'uid' => 2, 'organization_id' => 2, 'subdomain' => 'em2', 'custom_domain' => 'musique2.net', 'locale' => 'fr_FR', 'matomo_site_id' => null, 'template' => 'Classic', 'template_preferences' => '{"themeColor":"light-blue","displayCarousel":"1"}', 'organization_name' => 'Ecole de musique 2', 'deleted' => 0  ],
        [ 'uid' => 3, 'organization_id' => 3, 'subdomain' => '', 'custom_domain' => '', 'locale' => 'fr_FR', 'matomo_site_id' => null, 'template' => 'Classic', 'template_preferences' => '{"themeColor":"light-blue","displayCarousel":"1"}', 'organization_name' => 'Ecole de musique 3', 'deleted' => 1  ],
    ];

    /**
     * Returns a typical full stack of typo3 ot_websites
     * as it could be returned by:
     *
     *    $queryBuilder->getRestrictions()->removeAll();
     *    $queryBuilder->select('*')
     *                 ->from('ot_websites')
     *                 ->execute()
     *                 ->fetchAll();
     *
     * @return array[]
     */
    public function getAll(): array
    {
        return $this->websites;
    }

    /**
     * Returns a typical full stack of typo3 ot_websites
     * as it could be returned by:
     *
     *    $queryBuilder->select('*')
     *                 ->from('ot_websites')
     *                 ->execute()
     *                 ->fetchAll();
     *
     * @return array[]
     */
    public function getNonDeleted(): array
    {
        return array_filter($this->websites, function($p) { return $p['deleted'] == 0; });
    }

    /**
     * Returns a typo3 page
     * as it could be returned by:
     *
     *    $queryBuilder->select('*')
     *                 ->from('ot_websites')
     *                 ->where($queryBuilder->expr()->eq('uid', $websiteUid))
     *                 ->execute()
     *                 ->fetch();
     *
     * @param $websiteUid
     * @return array|null
     */
    public function getByUid($websiteUid): ?array
    {
        foreach ($this->websites as $website) {
            if ($website['uid'] == $websiteUid) {
                return $website;
            }
        }
        return null;
    }
}
