<?php
namespace App\Exports;

use App\Models\Member;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class MembersExport implements FromCollection, WithHeadings, WithMapping
{
    protected $status;
    protected $wg;
    protected $committee;
    protected $legalCommittee;
    protected $legalOpinions;
    protected $type;

    public function __construct($status = "", $wg = 0, $type = "", $committee = 0, $legalCommittee = 0, $legalOpinions = 0)
    {
        $this->status = $status;
        $this->wg = intval($wg);
        $this->committee = intval($committee);
        $this->legalCommittee = intval($legalCommittee);
        $this->legalOpinions = intval($legalOpinions);
        $this->type = $type;
    }

    public function collection()
    {
        $query = Member::query();

        if (!empty($this->status)) {
            $query->where('state', $this->status);
        }

        if (!empty($this->wg) && $this->wg > 0) {
            $wg = $this->wg;
            $query->whereExists(function ($query) use ($wg) {
                $query->select('work_group_id')
                    ->from('member_work_group')
                    ->where('work_group_id', $wg)
                    ->whereRaw('member_work_group.member_id = members.id');
            });
        }

        if (!empty($this->committee) && $this->committee > 0) {
            $committee = $this->committee;
            $query->whereExists(function ($query) use ($committee) {
                $query->select('committee_id')
                    ->from('committee_member')
                    ->where('committee_id', $committee)
                    ->whereRaw('committee_member.member_id = members.id');
            });
        }

        if (!empty($this->type)) {
            $query->where('member_type', $this->type);
        }

        if (!empty($this->legalCommittee)) {
            $query->where('legal_committee', $this->legalCommittee);
        }

        if (!empty($this->legalOpinions)) {
            $query->where('legal_opinions', $this->legalOpinions);
        }

        $query->orderBy('name', 'asc');
        return $query->get();
    }

    public function headings(): array
    {
        $headers = [
            'ID',
            'Name',
            'Notes',
            'Types',
            'Country Code',
            'State',
            'Users',
            'Legal Committee',
            'Legal Opinions',
            'Create Date',
            'Committees',
            'TF / WG',
        ];

        return $headers;
    }

    public function map($member): array
    {
        $committees = [];
        foreach ($member->committees as $value) {
            $committees[] = $value->title;
        }

        $workGroups = [];
        foreach ($member->workGroups as $value) {
            $workGroups[] = $value->title;
        }

        $types = [];

        if (!empty($member->member_type)) {
            $types[] = $member->member_type === 'regular' ? 'R' : 'Assoc.';
        }

        return [
            $member->id,
            $member->name,
            $member->notes,
            implode(', ', $types),
            $member->country->code,
            $member->state,
            (int) $member->users->count(),
            !empty($member->legal_committee) ? 'yes' :  'no',
            !empty($member->legal_opinions) ? 'yes' :  'no',
            $member->created_at->toDateTimeString(),
            implode(', ', $committees),
            implode(', ', $workGroups),
        ];
    }
}
