<?php

namespace App\Models;
use App\Classes\General\Interfaces\Documentable;
use App\Classes\Modules\ServiceTypes\DataTransferObjects\ServiceConfigurationsObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;


/**
 * Class Company
 * @package App\Models
 *
 * @property \App\Models\Country country_id
 * @property \App\Models\State state_id
 * @property \App\Models\District district_id
 * @property string postcode
 * @property string street_one
 * @property string street_two
 * @property integer billing_type
 */
class Company extends AbstractModel implements Documentable
{
    use HasRelationships;
    use SoftDeletes;

    protected $table = 'companies';

    protected $dates = ['deleted_at', 'created_at'];


    /**
     * @return HasMany
     */
    public function contacts(): HasMany
    {
        return $this->HasMany(Contact::class, 'company_id');
    }

    /**
     * @return HasMany
     */
    public function addresses(): HasMany
    {
        return $this->HasMany(Address::class, 'company_id');
    }

    /**
     * @return belongsToMany
     */
    public function segments(): belongsToMany
    {
        return $this->belongsToMany(Segment::class, (new SegmentCompany())->getTable(), 'company_id', 'segment_id');
    }

    /**
     * @return belongsToMany
     */
    public function seasonalSegments()
    {
        return $this->hasMany(SeasonalSegment::class);
    }

    /**
     * @return belongsToMany
     */
    public function employees(): belongsToMany
    {
        return $this->belongsToMany(User::class, (new Employee())->getTable(), 'company_id','user_id');
    }

    /**
     * @return MorphMany
     */
    public function documents(): morphMany
    {
        return $this->morphMany(Document::class, 'owner');
    }

    /**
     * @return HasMany
     */
    public function banks(): HasMany
    {
        return $this->HasMany(Bank::class, 'company_id');
    }

    /**
     * @return HasMany
     */
    public function bookings(): HasMany
    {
        return $this->HasMany(Booking::class, 'company_id');
    }

    /**
     * @return hasManyDeep
     */
    public function transactions(): hasManyDeep
    {
        return $this->hasManyDeep(Transaction::class, [Booking::class], ['company_id',  'owner_id'], ['id', 'id']);
    }

    /**
     * @return HasMany
     */
    public function issuerTransactions(): HasMany
    {
        return $this->HasMany(Transaction::class, 'issuer');
    }

    /**
     * @return MorphMany
     */
    public function wallets(): morphMany
    {
        return $this->morphMany(Wallet::class, 'owner');
    }

    /**
     * @return Builder
     */
    public function services(): Builder  {

        if ($this->business_type === 3) {
            return ServiceType::where('status', ApprovalStatus::APPROVED)->whereHas('constants', function($query) {
                $query->where(function($query) {
                    $query->where('reference', SegmentConstants::SERVICE_TYPE)->where('detail->is_active', true);
                })->orWhere(function($query) {
                    $query->where('reference', SegmentConstants::CUSTOM_SERVICE_TYPE)->where('detail->is_active', true);
                });
            });
        } else {
            // Existing logic for other company types
            return ServiceType::where('status', ApprovalStatus::APPROVED)->whereHas('constants', function($query) {
                $query->Where(function($query){
                    $query->where('reference', SegmentConstants::SERVICE_TYPE)->where('detail->is_active', true);
                })->orWhere(function($query) {
                    $query->where('reference', SegmentConstants::CUSTOM_SERVICE_TYPE)->where('detail->is_active', true)->whereIn('segment_id', $this->segments->pluck('id'));
                });
            });
        }
    }

    public function servicesConfigurations(): Collection {
        return $this->services()->get()->map(function($service)  {
            return new ServiceConfigurationsObject($service,
                $service->constants->where('reference', SegmentConstants::SERVICE_TYPE)->first(),
                $service->constants->where('reference', SegmentConstants::CUSTOM_SERVICE_TYPE)->whereIn('segment_id', $this->segments->pluck('id')));
        });
    }

}



