<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class CorporateAccount extends Model
{
    use HasFactory;

    protected $fillable = [
        'tenant_id',
        'user_id',
        'company_name',
        'company_registration_number',
        'tax_id',
        'industry',
        'company_size',
        'address',
        'city',
        'state',
        'country',
        'postal_code',
        'phone',
        'website',
        'status',
        'account_manager_id',
        'total_employees',
        'total_travelers',
        'monthly_budget',
        'contract_start_date',
        'contract_end_date',
    ];

    protected $casts = [
        'monthly_budget' => 'decimal:2',
        'contract_start_date' => 'date',
        'contract_end_date' => 'date',
    ];

    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function accountManager(): BelongsTo
    {
        return $this->belongsTo(User::class, 'account_manager_id');
    }

    public function travelers(): HasMany
    {
        return $this->hasMany(CorporateTraveler::class);
    }

    public function travelPolicies(): HasMany
    {
        return $this->hasMany(TravelPolicy::class);
    }

    public function departmentBudgets(): HasMany
    {
        return $this->hasMany(DepartmentBudget::class);
    }

    public function crmQuotes(): HasMany
    {
        return $this->hasMany(CrmQuote::class);
    }

    public function crmContracts(): HasMany
    {
        return $this->hasMany(CrmContract::class);
    }

    public function corporateClients(): HasMany
    {
        return $this->hasMany(CorporateClient::class);
    }

    public function marketingCampaigns(): HasMany
    {
        return $this->hasMany(MarketingCampaign::class);
    }

    public function customerSurveys(): HasMany
    {
        return $this->hasMany(CustomerSurvey::class);
    }

    public function users(): HasMany
    {
        return $this->hasMany(User::class);
    }

    public function adminUser()
    {
        return $this->hasOne(User::class)->where('role', 'Corporate Admin');
    }
}
