<?php

namespace App\Services;

class ReviewerConflictService
{
    public static function detect(
        array $submission,
        array $reviewer
    ): array
    {
        $conflicts=[];

        $authorEmail=
        strtolower(
        trim(
        (string)($submission['author_email'] ?? '')
        ));

        $reviewerEmail=
        strtolower(
        trim(
        (string)($reviewer['email'] ?? '')
        ));

        $authorDomain='';
        $reviewerDomain='';

        if(strpos($authorEmail,'@')!==false){
            $authorDomain=
            substr(
            strrchr($authorEmail,'@'),
            1
            );
        }

        if(strpos($reviewerEmail,'@')!==false){
            $reviewerDomain=
            substr(
            strrchr($reviewerEmail,'@'),
            1
            );
        }

        if(
            $authorDomain
            &&
            $reviewerDomain
            &&
            $authorDomain === $reviewerDomain
        ){
            $conflicts[]=[
                'type'=>'same_domain',
                'reason'=>'Author and reviewer share email domain',
                'risk'=>90
            ];
        }

        return $conflicts;
    }
}
