<?php

namespace App;

use App\Laravue\Models\User;
use Illuminate\Database\Eloquent\Model;

class ServiceRequest extends Model
{
    protected $table = 'service_requests';

    const TYPE_LISTING = 0;          // 出品申請
    const TYPE_LISTING_STOP = 1;     // 出品停止
    const TYPE_SERVICE_STOP = 2;     // 利用停止
    const TYPE_TEMPORARY_RETURN = 3; // 一時返却

    const STATUS_PENDING = 0;    // 申請中
    const STATUS_IN_PROGRESS = 1; // 対応中
    const STATUS_COMPLETED = 2;  // 完了
    const STATUS_REJECTED = 3;   // 却下
    const STATUS_CANCELLED = 4;  // キャンセル

    protected $fillable = [
        'user_id', 'product_id', 'type', 'status',
        'content', 'delivery_date', 'return_date',
        'admin_note', 'completed_at',
    ];

    protected $casts = [
        'delivery_date' => 'date:Y-m-d',
        'return_date' => 'date:Y-m-d',
        'completed_at' => 'datetime',
    ];

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

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }

    public static function typeLabel(int $type): string
    {
        return [
            self::TYPE_LISTING => '出品申請',
            self::TYPE_LISTING_STOP => '出品停止',
            self::TYPE_SERVICE_STOP => '利用停止',
            self::TYPE_TEMPORARY_RETURN => '一時返却',
        ][$type] ?? '不明';
    }

    public static function statusLabel(int $status): string
    {
        return [
            self::STATUS_PENDING => '申請中',
            self::STATUS_IN_PROGRESS => '対応中',
            self::STATUS_COMPLETED => '完了',
            self::STATUS_REJECTED => '却下',
            self::STATUS_CANCELLED => 'キャンセル',
        ][$status] ?? '不明';
    }
}
