use strict;
use warnings;
my @OUR_QUEUES = ('Incidents', 'Incident Reports', 'Investigations', 'Blocks');

our @Initial = (
    sub {
        return 1 if grep $_ eq 'RT::IR', RT->Config->Get('Plugins');

        die "Please enable RT::IR plugin in the config file.\n";
    },
);

our @ACL;
foreach my $queue (@OUR_QUEUES) {
    push @ACL, (
    { GroupId => 'DutyTeam',
      GroupDomain => 'UserDefined',
      Queue => $queue,
      Right  => 'ShowOutgoingEmail', },
 );
}

our @Final = (

    sub {
        $RT::Logger->debug("Going to make IP custom fields link to Lookup");
        my $ip_cf = RT::CustomField->new(RT->SystemUser);
        my ($ok, $msg) = $ip_cf->Load('IP');
        if ( $ip_cf->Id ) {
            if ( $ip_cf->LinkValueTo ){
                $RT::Logger->warning("IP custom field link value already set, skipping");
            }
            else{
                $ip_cf->SetLinkValueTo('__WebPath__/RTIR/Tools/Lookup.html?type=ip&q=__CustomField__&ticket=__id__');
            }
        }
        else{
            $RT::Logger->error("IP custom field not found: $msg");
        }
    },

    # fix our standard SelectSingle Custom Fields to use a more modern dropdown
    sub {
       for my $name ('Constituency', 'Resolution', 'Function', 'Classification', 'How Reported', 'Reporter Type', 'Customer') {
            my $cf = RT::CustomField->new( RT->SystemUser );
            # We have to be ugly here because Customers exists twice, once as a single value and once as a multiple
            my ($ok, $msg) = $cf->LoadByCols(Name => $name, MaxValues => 1);
            unless ($ok) {
                RT->Logger->error("Unable to load a Single Value Select named $name: $msg - skipping update of Render Type");
                next;
            } 

            ($ok,$msg) = $cf->SetRenderType('Dropdown');
            unless ($ok) {
                RT->Logger->error("Unable to set Render Type to Dropdown for $name: $msg");
            }
        }
    },
);

1;
