<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * "Sizin için bulalım" talebi. company_id null → genel (tüm firmalar görür).
 */
class PropertyRequest extends Model
{
    public const STATUS_NEW = 'new';
    public const STATUS_CONTACTED = 'contacted';
    public const STATUS_CLOSED = 'closed';

    public const STATUSES = [
        self::STATUS_NEW,
        self::STATUS_CONTACTED,
        self::STATUS_CLOSED,
    ];

    protected $fillable = [
        'user_id',
        'company_id',
        'category_id',
        'category_name',
        'location',
        'price_min',
        'price_max',
        'currency',
        'description',
        'name',
        'email',
        'phone',
        'locale',
        'source',
        'status',
        'contacted_at',
    ];

    protected $casts = [
        'price_min' => 'decimal:2',
        'price_max' => 'decimal:2',
        'contacted_at' => 'datetime',
    ];

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

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function category()
    {
        return $this->belongsTo(ListingCategory::class, 'category_id');
    }

    /** Firmanın görebildiği talepler: kendisine özel + genel. */
    public function scopeVisibleToCompany($query, int $companyId)
    {
        return $query->where(function ($q) use ($companyId) {
            $q->where('company_id', $companyId)->orWhereNull('company_id');
        });
    }
}
