<?php

namespace App\Models\Communication;

use App\Models\Solicitor\Solicitor;
use App\Models\Synapse\CallScore;
use App\Models\Synapse\SynapseTranscript;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Storage;

class Recording extends Model {
    protected $fillable = [
        'communication_id',
        'call_id',
        'solicitor_id',
        'provider',
        'provider_call_sid',
        'provider_recording_sid',
        'status',
        'duration',
        'recording_url',
        'storage_disk',
        'storage_path',
        'recorded_at',
        'meta',
    ];
    protected $casts = [
        'meta' => 'array',
        'recorded_at' => 'datetime',
        'duration' => 'integer',
    ];

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    */

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

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

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

    public function synapseTranscript(): HasOne {
        return $this->hasOne(SynapseTranscript::class);
    }

    public function callScore(): HasOne {
        return $this->hasOne(CallScore::class);
    }

    /*
    |--------------------------------------------------------------------------
    | Scopes
    |--------------------------------------------------------------------------
    */

    public function scopeForSolicitor(Builder $query, int $solicitorId): Builder {
        return $query->where('solicitor_id', $solicitorId);
    }

    public function scopeForCommunication(Builder $query, int $communicationId): Builder {
        return $query->where('communication_id', $communicationId);
    }

    public function scopeForCall(Builder $query, int $callId): Builder {
        return $query->where('call_id', $callId);
    }

    public function scopeProvider(Builder $query, string $provider): Builder {
        return $query->where('provider', $provider);
    }

    public function scopeCompleted(Builder $query): Builder {
        return $query->where('status', 'completed');
    }

    /*
    |--------------------------------------------------------------------------
    | Helpers
    |--------------------------------------------------------------------------
    */

    public function hasExternalUrl(): bool {
        return filled($this->recording_url);
    }

    public function getPlaybackUrlAttribute(): ?string {
        if ($this->hasStoredFile()) {
            return Storage::disk($this->storage_disk)->url($this->storage_path);
        }

        return $this->recording_url;
    }

    public function hasStoredFile(): bool {
        return filled($this->storage_disk) && filled($this->storage_path);
    }

    public function getDurationLabelAttribute(): string {
        $seconds = (int)($this->duration ?? 0);

        $minutes = floor($seconds / 60);
        $remainingSeconds = $seconds % 60;

        return sprintf('%02d:%02d', $minutes, $remainingSeconds);
    }

    public function getStatusLabelAttribute(): string {
        return str((string)$this->status)->replace('_', ' ')->title()->toString();
    }
}
