<?php

namespace App\Repository;

use App\PlanningPrice;
use App\RoomCount;
use App\RoomType;

class PlanningPriceRepository
{
    public function generalPrice($residentialComplexId)
    {
        $query = PlanningPrice::query()
            ->with('room_type')
            ->where('residential_complex_id', $residentialComplexId)
            ->whereNull('section_id')
            ->orderBy('room_type_id')
            ->orderBy('room_count_id');

        return $query->get();
    }

    public function fullPrice($residentialComplexId)
    {
        $query = PlanningPrice::query()
            ->where('residential_complex_id', $residentialComplexId)
            ->orderBy('room_type_id')
            ->orderBy('room_count_id');

        return $query->get();
    }

    public function showFilterParams($parameters = [])
    {
        $query = PlanningPrice::query();
        $query->whereNull('prices.deleted_at');
        if (isset($parameters['residential_complex_filter'])) {
            $query
                ->selectRaw("min(case when (prices.deleted_at is null) then prices.min_space end)
                    as min_space")
                ->selectRaw("max(case when (prices.deleted_at is null) then prices.max_space end)
                    as max_space")
                ->selectRaw("max(case when (prices.deleted_at is null) then prices.min_space end)
                    as max_from_min_space")
                ->selectRaw("min(case when (prices.deleted_at is null) then prices.min_price end)
                    as min_price")
                ->selectRaw("max(case when (prices.deleted_at is null) then prices.max_price end)
                    as max_price")
                ->selectRaw("max(case when (prices.deleted_at is null) then prices.min_price end)
                    as max_from_min_price");
        }

        return $query->get();
    }
}
