<?php

namespace App\Console\Commands;

use App\Models\Committee;
use Illuminate\Support\Facades\DB;
use Illuminate\Console\Command;

class ActivateLegalCommitteeUsersToLegalOpinions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'activate:users-legal-opinions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Automatically activate all Legal Committee users to Legal Opinions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $legalCommittee = Committee::where('type', 'legalCommittee')->first();
        $legalOpinion = Committee::where('type', 'legalOpinions')->first();

        // Find all companies (members) that are part of the Legal Committee
        $legalCommitteeMembers = DB::table('committee_member')
            ->where('committee_id', $legalCommittee->id)
            ->pluck('member_id');

        // Find companies that are also part of the Legal Opinions
        $legalOpinionsMembers = DB::table('committee_member')
            ->where('committee_id', $legalOpinion->id)
            ->pluck('member_id');

        // Filter members who are part of both committees
        $commonMembers = $legalCommitteeMembers->intersect($legalOpinionsMembers);

        // Find all users whose companies are part of both the Legal Committee and Legal Opinions
        $usersInLegalCommitteeAndOpinionsCompanies = DB::table('users')
            ->whereIn('member_id', $commonMembers)
            ->where('state', 'active')
            ->pluck('id');

        // Find users already registered for Legal Committees
        $usersInLegalCommittees = DB::table('committee_user')
            ->where('committee_id', $legalCommittee->id)
            ->pluck('user_id');

        // Get the users that need to be added to Legal Opinions
        $usersToActivateToLegalOpinionCommittee = $usersInLegalCommitteeAndOpinionsCompanies
            ->intersect($usersInLegalCommittees) // Only users in the legal committee
            ->diff(DB::table('committee_user')
            ->where('committee_id', $legalOpinion->id)
            ->pluck('user_id'));


        foreach ($usersToActivateToLegalOpinionCommittee as $userId) {
            DB::table('committee_user')->insert([
                'committee_id' => $legalOpinion->id,
                'user_id'      => $userId,
                'position'     => null,
                'type'         => 'to'
            ]);
        }

        if ($usersToActivateToLegalOpinionCommittee->isNotEmpty()) {
            $this->info('Users (' . implode(', ', $usersToActivateToLegalOpinionCommittee->toArray()) . ') successfully added to Legal Opinions committee.');
        } else {
            $this->info('No users were added to Legal Opinions committee.');
        }

    }
}
