<?php

namespace App\Builders;

use Illuminate\Database\Eloquent\Builder;

class MemberBuilder extends Builder
{
    public function filter(array $filters): static
    {
        return $this->when($filters['search'] ?? false, function (MemberBuilder $query, $search) {
            $query->where('member_id', 'like', "%" . $search . "%")
                ->orWhere('payor_id', 'like', "%" . $search . "%")
                ->orWhere('email', 'like', "%" . $search . "%")
                ->orWhereHas('person', function ($query) use ($search) {
                    $query->where('name', 'like', "%" . $search . "%");
                    $query->orWhere('phone', 'like', "%" . $search . "%");
                })
                ->orWhereHas('currentPlan', function ($query) use ($search) {
                    $query->where('code', 'like', "%" . $search . "%");
                });
        });
    }

    public function joinCorporateEmployees(string $value = 'join'): static
    {
        // return match ($value) {
        //     'join' => $this->join('corporate_employees', 'members.id', '=', 'corporate_employees.member_id'),
        //     'left' => $this->leftJoin('corporate_employees', 'members.id', '=', 'corporate_employees.member_id')
        // };

        return match ($value) {
            'join' => $this->join('corporate_employees', function ($join) {
                $join->on('members.id', '=', 'corporate_employees.member_id')
                    ->orWhere('members.principal_id', '=', 'corporate_employees.member_id');
            }),
            'left' => $this->leftJoin('corporate_employees', function ($join) {
                $join->on('members.id', '=', 'corporate_employees.member_id')
                    ->orWhere('members.principal_id', '=', 'corporate_employees.member_id');
            }),
            'right' => $this->rightJoin('corporate_employees', function ($join) {
                $join->on('members.id', '=', 'corporate_employees.member_id')
                    ->orWhere('members.principal_id', '=', 'corporate_employees.member_id');
            })
        };
    }

    public function joinCorporateDivisions(string $value = 'join'): static
    {
        return match ($value) {
            'join' => $this->join('corporate_divisions', 'corporate_employees.division_id', '=', 'corporate_divisions.id'),
            'left' => $this->leftJoin('corporate_divisions', 'corporate_employees.division_id', '=', 'corporate_divisions.id')
        };
    }

    public function joinClaimRequests(string $value = 'join'): static
    {
        // return match ($value) {
        //     'join' => $this->join('claim_requests', 'members.id', '=', 'claim_requests.member_id'),
        //     'left' => $this->leftJoin('claim_requests', 'members.id', '=', 'claim_requests.member_id'),
        //     // 'right' => $this->rightJoin('claim_requests', 'members.id', '=', 'claim_requests.member_id')
        //     'right' => $this->rightJoin('claim_requests', function ($join) {
        //         $join->on('members.id', '=', 'claim_requests.member_id')
        //              ->orWhere('members.principal_id', '=', 'claim_requests.member_id');
        //     })
        // };

        return match ($value) {
            'join' => $this->join('claim_requests', function ($join) {
                $join->on('members.id', '=', 'claim_requests.member_id')
                    ->orWhere('members.principal_id', '=', 'claim_requests.member_id');
            }),
            'left' => $this->leftJoin('claim_requests', function ($join) {
                $join->on('members.id', '=', 'claim_requests.member_id')
                    ->orWhere('members.principal_id', '=', 'claim_requests.member_id');
            }),
            'right' => $this->rightJoin('claim_requests', function ($join) {
                $join->on('members.id', '=', 'claim_requests.member_id')
                    ->orWhere('members.principal_id', '=', 'claim_requests.member_id');
            })
        };
        
    }

    public function joinMemberPlans(string $value = 'join'): static
    {
        return match ($value) {
            'join' => $this->join('member_plans', 'members.id', '=', 'member_plans.member_id'),
            'left' => $this->leftJoin('member_plans', 'members.id', '=', 'member_plans.member_id'),
        };
    }

    public function joinPlans(string $value = 'join'): static
    {
        return match ($value) {
            'join' => $this->join('plans', 'member_plans.plan_id', '=', 'plans.id'),
            'left' => $this->leftJoin('plans', 'member_plans.plan_id', '=', 'plans.id'),
        };
    }
}
