package Distribution::DB::Item::ServiceAccount;
use strict;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use Common::Assert;

use base 'Common::DB::Item';

use constant kTable => 'service_account';
use constant kDB    => Common::DB::Item::kCommonDB;

sub LookupWithClient {
    my $class              = shift;
    my %args               = @_;
    my $service_account_id = $args{service_account_id};
    my $client_id          = $args{client_id};
    my $service_id         = $args{service_id};

    my $dbo = Common::RSApp::GetCommonDB();

    my $where;
    my $join = $client_id ? " AND client_id = " . $dbo->DBQuote($client_id) : " AND client_id IS NULL ";

    $where = " service_account.service_account_id = " . $dbo->DBQuote($service_account_id) . " "
      if ($service_account_id);

    if ($service_id) {
        $where = " AND " if ($where);
        $where = " distribution_service.service_id = " . $dbo->DBQuote($service_id) . " ";
    }

    $where = $where ? " WHERE $where " : "";

    my $sql =
        "SELECT service_account.*, client_id "
      . "FROM distribution_service "
      . "LEFT JOIN service_account using (service_id) "
      . "LEFT JOIN client_service_account_map ON ( client_service_account_map.service_account_id = service_account.service_account_id $join ) "
      . "$where";

    Common::Log::Debug("QUERY: $sql");
    return $class->GetAll($sql);
}

sub GetAccountType {
    my ( $class, %args ) = @_;
    my $dbo = Common::RSApp::GetCommonDB();

    assert( $args{service_id} );
    assert( $args{client_id} );

    my $sql =
        "SELECT account_type "
      . "FROM service_account "
      . "INNER JOIN client_service_account_map USING (service_account_id) "
      . "WHERE client_id = "
      . $dbo->DBQuote( $args{client_id} )
      . " AND service_id = "
      . $dbo->DBQuote( $args{service_id} );

    Common::Log::Debug("QUERY: $sql");

    my $sth = $dbo->DoCmd($sql);
    my ($type) = $sth->fetchrow_array();

    return $type;
}

sub GetAccountInfo {
    my ( $class, %args ) = @_;
    my $dbo = Common::RSApp::GetCommonDB();

    assert( $args{service_id} );
    assert( $args{client_id} );

    my $sql =
        "SELECT a.account_name, a.remote_base_path, c.sfx_acct_name, "
      . "username, password, hostname, authfile, port, protocol "
      . "FROM client_service_account_map m, service_account a, service_credential c "
      . "WHERE m.service_account_id = a.service_account_id AND "
      . "a.service_credential_id = c.service_credential_id AND "
      . "m.client_id = "
      . $dbo->DBQuote( $args{client_id} ) . " AND "
      . "a.service_id = "
      . $dbo->DBQuote( $args{service_id} );

    my $sth = $dbo->DoCmd($sql);
    my ( $code, $basepath, $sfxacct, $uname, $passwd, $host, $authfile, $port, $protocol ) = $sth->fetchrow_array();
    my %info;
    $info{code}     = $code;
    $info{port}     = $port;
    $info{protocol} = $protocol;
    $info{basepath} = $basepath;
    $info{sfxacct}  = $sfxacct;
    $info{username} = $uname;
    $info{password} = $passwd;
    $info{hostname} = $host;
    $info{authfile} = $authfile;

    return \%info;
}

1;
