<?php

namespace App\Http\Controllers;

use App\Services\MemberService;
use App\Services\UserService;
use App\Services\ContributionService;
use App\Http\Requests\Member\ContributionStore;
use Illuminate\Support\Facades\Mail;
use App\Mail\MemberChanges;

class ContributionController extends Controller
{

    private $contributionService;
    private $userService;

    public function __construct(
        MemberService $memberService,
        UserService $userService,
        ContributionService $contributionService
    ) {
        $this->memberService = $memberService;
        $this->userService = $userService;
        $this->contributionService = $contributionService;
        $this->middleware('auth:sanctum');
    }

    public function index()
    {
        $contributions = $this->contributionService->getAll();

        return response()->json($contributions, 201);
    }

    public function store(ContributionStore $request)
    {
        $member = $this->memberService->get($request->member_id);

        if (!$member) {
            return response()->json(['message' => 'Member not found.'], 404);
        }

        $isSuperAdmin = auth()->user()->hasRole('super_admin');
        $isAdmin = auth()->user()->hasRole('admin');
        $isSuperAdminOrAdmin = $isSuperAdmin || $isAdmin;

        $isAppliedToAll = $request->has('toAll') ? true : false;

        if (!$isSuperAdminOrAdmin) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        $request_data = $request->validated();        

        $request_data['revisor_id'] = auth()->user()->id;
        $request_data['approved_at'] = now();

        $contribution = null;

        if ($isAppliedToAll) {            

            $members = $this->memberService->getAll();

            //  Store for all existing members
            foreach ($members as $member) {

                $request_data['member_id'] = $member['id'];

                if ($request_data['member_id'] === $member['user_id']) {
                    $contribution = $this->contributionService->save($request_data);
                } else {
                    $this->contributionService->save($request_data);
                }
            }
        } else {
            $contribution = $this->contributionService->save($request_data);
        }

        return response()->json($contribution, 201);
    }

    public function show($id)
    {
        $contribution = $this->contributionService->get($id);

        return response()->json($contribution);
    }

    public function destroy($id)
    {
        $isSuperAdmin = auth()->user()->hasRole('super_admin');
        $isAdmin = auth()->user()->hasRole('admin');
        $isSuperAdminOrAdmin = $isSuperAdmin || $isAdmin;

        if (!$isSuperAdminOrAdmin) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        $this->contributionService->delete($id);
        return response()->json(null, 204);
    }
}
