<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TeleRecord extends Model
{
    use SoftDeletes;

   protected $fillable = [
    'outlet_id','created_by','name','record_no','phone',
    'audio_path','audio_mime','audio_size','called_at',
    'notes', 
];

    protected $casts = [
        'called_at' => 'datetime',
    ];

    public function outlet() { return $this->belongsTo(Outlet::class); }
    public function creator() { return $this->belongsTo(User::class, 'created_by'); }

    // Scope owner for telecalling users
    public function scopeForUser($q, $user)
    {
        if (!$user) return $q->whereRaw('1=0');
        if ($user->role !== 'admin') {
            return $q->where('created_by', $user->id);
        }
        return $q;
    }
}
