<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class MemberResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'slug' => $this->slug,
            'type' => $this->type,
            'branch_id' => $this->branch_id,
            'main_branch' => $this->main_branch->title ?? null,
            'informal_name' => $this->informal_name,
            'formal_name' => $this->formal_name,
            'kvk_number' => $this->kvk_number,
            'website' => $this->website,
            'logo' =>  [
                'full' => $this->getFirstMediaUrl('members_logos'),
                'thumb' => $this->getFirstMediaUrl('members_logos', 'thumb')
            ],
            'show_on_website' => $this->show_on_website,
            'helpdesk_department' => $this->helpdesk_department,
            'helpdesk_contact_person' => $this->helpdesk_contact_person,
            'helpdesk_email' => $this->helpdesk_email,
            'helpdesk_phone' => $this->helpdesk_phone,
            'info_department' => $this->info_department,
            'info_contacteperson' => $this->info_contacteperson,
            'info_email' => $this->info_email,
            'info_phone' => $this->info_phone,
            'info_address' => $this->info_address,
            'info_housenumber' => $this->info_housenumber,
            'info_postal' => $this->info_postal,
            'info_city' => $this->info_city,
            'info_country' => $this->info_country,
            'info_link' => $this->info_link,
            'more_info_link' => $this->more_info_link,

            'summaries' => $this->whenLoaded('summaries'),
            'addresses' => $this->whenLoaded('addresses'),
            'contacts' => $this->whenLoaded('contacts'),
            'contributions' => $this->whenLoaded('contributions'),
            'management_links' => $this->whenLoaded('management_links'),
            'sub_branches' => $this->whenLoaded('sub_branches'),
            'user_ids' => $this->relationLoaded('users')
                ? $this->users->pluck('id')
                : [],

            'revision' => $this->whenLoaded('revision'),

            'start_membership' => $this->start_membership,
            'end_membership' => $this->end_membership,
            'contribution' => $this->contribution,
            'invoice_number' => $this->invoice_number,

            'created_at' => $this->created_at->toDateTimeString(),
            'updated_at' => $this->updated_at ? $this->updated_at->toDateTimeString() : null,
            'deleted_at' => $this->deleted_at ? $this->deleted_at->toDateTimeString() : null,
        ];
    }
}
