use strict;
use warnings;

use List::MoreUtils 'uniq';

our @Initial = (
    # migrate old Recipient field to new Recipients format
    sub {
        $RT::Logger->debug("Going to migrate dashboard subscription recipients");

        my $attrs = RT::Attributes->new( RT->SystemUser );
        $attrs->Limit( FIELD => 'ObjectType', VALUE => 'RT::User' );
        $attrs->Limit( FIELD => 'Name', VALUE => 'Subscription' );

        while ( my $attr = $attrs->Next ) {
            my %fields = ( Recipients => { Users => [], Groups => [] } );

            my $recipient = $attr->SubValue('Recipient');
            my @users;

            if ($recipient) {
                for ( RT::EmailParser->ParseEmailAddress($recipient) ) {
                    my ( $email, $name ) = ( $_->address, $_->name );

                    my $user = RT::User->new(RT->SystemUser);
                    $user->LoadOrCreateByEmail(
                        EmailAddress => $email,
                        RealName     => $name,
                        Comments     => 'Autocreated when added as a dashboard subscription recipient',
                    );

                    push @users, $user->id;
                }
            } else { # blank recipient represents dashboard creator subscription
                push @users, $attr->ObjectId;
            }

            @{ $fields{Recipients}->{Users} } = uniq @users;

            my ($ok, $msg) = $attr->SetSubValues(%fields);
            unless ($ok) {
                $RT::Logger->error("Couldn't update subscription: $msg");
                $RT::Logger->error("Aborting dashboard subscription recipient migration");
                exit;
            }

            ($ok, $msg) = $attr->DeleteSubValue('Recipient');
            $RT::Logger->error("Couldn't delete Recipient field from subscription: $msg") unless $ok;
        }
        return 1;
    },
    # fix incompletely-saved charts
    sub {
        my $attrs = RT::Attributes->new(RT->SystemUser);
        $attrs->Limit( FIELD => 'Name', VALUE => 'SavedSearch' );
        while ( my $attr = $attrs->Next ) {
            my $content = $attr->Content;

            next unless $content->{SearchType}
                and lc($content->{SearchType}) eq 'chart';

            next if $content->{ChartStyle}
                 && $content->{GroupBy}
                 && $content->{ChartFunction};

            $content->{ChartStyle} ||= 'bar+table+sql';
            $content->{GroupBy} ||= ['Status'];
            $content->{ChartFunction} ||= ['COUNT'];

            my ($ret, $msg) = $attr->SetContent($content);
            unless ( $ret ) {
                RT->Logger->error("Failed to update chart for SavedSearch #" . $attr->id . ": $msg");
            }
        }
    },
);

