<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Report extends BaseModel
{
    use HasFactory;

    protected $table = 'reports';

    protected $fillable = [
        'id', 'code', 'report_date', 'plan_detail_id', 'customer_id', 'charge_id',
        'primary_member', 'second_member', 'car', 'notice', 'request', 'on_site_cost', 'address', 'estimated_workload', 'has_estimated',
        'is_finished', 'user_id', 'leader_id', 'reporter_id', 'progressing_user_id', 'report_created_at', 'is_allowed_single_report',
        'is_record', 'is_new', 'is_activated', 'is_deleted', 'created_at', 'updated_at'
    ];

    public function detail() {
        return $this->hasMany(ReportDetail::class, 'report_id', 'id');
    }

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

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

    public function customer() {
        return $this->belongsTo(Customer::class, 'customer_id', 'id')
            ->with('project')
            ->select('id', 'project_id', 'name', 'first_name', 'last_name', 'furigana_first_name', 'furigana_last_name');
    }

    public function charge() {
        return $this->belongsTo(Charge::class, 'charge_id', 'id')
			->with('project')
            ->select('id', 'project_id', 'name', 'first_name', 'last_name', 'furigana_first_name', 'furigana_last_name');
    }

    public function planDetail() {
        return $this->belongsTo(PlanDetail::class, 'plan_detail_id', 'id')
            ->with(['plan', 'customer', 'charge', 'users']);
    }
}
