<?php

namespace App\Http\Resources;

use App\Models\Material;
use App\Models\Material_group;
use App\Models\Material_type;
use Illuminate\Http\Resources\Json\JsonResource;

class ConfigResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'type' => $this->type,
            'config_name' => $this->get_name($this->type, $this->type_code),
            'type_code' => $this->type_code,
        ];
    }

    private function get_name($type, $code)
    {
        switch ($type) {
            case 'type':
                $type = Material_type::where('material_type_code', $code)->first();
                if ($type) {
                    return $type->material_type_name . " Report";
                }
                return "";
                break;
            case 'group':
                $group = Material_group::where('material_group_code', $code)->first();
                if ($group) {
                    return $group->material_group_name . " Report";
                }
                return "";
                break;
            case 'material':
                $mat = Material::where('material_no', $code)->first();
                if ($mat) {
                    return $mat->material_name . " Report";
                }
                return "";
                break;
            default:
                return "Report";
                break;
        }
    }
}