<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * This model handles all the responses of the ussd as clients interact with the ussd service and submit responses.
 * The responses are stored into ussd_response table via the ussd backend api.
 * Class UssdResponse
 * @package App
 */
class UssdResponse extends Model
{
    /**
     * @var array
     */
    protected $fillable = [
        'tqc', //total question count for a ussd service
        'code', //ussd extension ie *899*12#
        'sequence_no',
        'msisdn',
        'question_id',
        'question_no',
        'response', //response from the user
    ];

    /**
     * Record belongs to a question
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function ussd_question() {
        return $this->belongsTo(UssdQuestion::class, 'question_id');
    }

    /**
     * Get the total no of people who responded to the ussd service;  where code = service code
     * @param $query
     * @param UssdService $service
     * @return mixed
     */
    public function scopeTotalResponses($query, UssdService $service) {
        return $query->select('sequence_no')->distinct()->where('code', $service->code)->pluck('sequence_no');
    }
}
