#!/usr/bin/perl i-w

package ippToPsps::Gpc1Db;

use warnings;
use strict;

use ippToPsps::MySQLDb;
our @ISA = qw(ippToPsps::MySQLDb);    # inherits from MySQLDb

###########################################################################
#
# Returns path to stack-stage cmf files for this sky_id
#
###########################################################################
sub getStackStageCmfs {
    my ($self, $skyId, $nebPath, $numInputs) = @_;

    my $query = $self->{_db}->prepare(<<SQL);
    SELECT path_base, num_inputs
        FROM staticskyResult
        WHERE sky_id = $skyId
SQL

    $query->execute;
    my $rows = $query->fetchall_arrayref();

    if (@{$rows} < 1 || @{$rows} > 1) {return 0;}

    my $row;
    foreach $row ( @{$rows} ) {
        (${$nebPath}, ${$numInputs}) = @{$row};
    }

    return 1;
}

###########################################################################
#
# Returns a count of warps thyat contributed to the stack with this stack ID
#
###########################################################################
sub countContributingWarpsToStack {
        my ($self, $stackId) = @_;

          my $query = $self->{_db}->prepare(<<SQL);
          SELECT COUNT(DISTINCT warp_id) 
              FROM stackInputSkyfile 
              WHERE stack_id = $stackId
SQL

    $query->execute;
    return scalar $query->fetchrow_array();
}

###########################################################################
#
# Returns diff-stage cmf files for this exposure
#
###########################################################################
sub getDiffStageCmfs {
    my ($self, $expId) = @_;

    # DESC and LIMIT 1 is to make sure we get most recently processed
    my $query = $self->{_db}->prepare(<<SQL);
    SELECT DISTINCT diffSkyfile.path_base
        FROM warpRun
        JOIN fakeRun USING(fake_id)
        JOIN camRun USING(cam_id)
        JOIN chipRun USING(chip_id)
        JOIN rawExp USING (exp_id)
        JOIN diffInputSkyfile ON (warpRun.warp_id = diffInputSkyfile.warp1 OR warpRun.warp_id = diffInputSkyfile.warp2)
        JOIN diffSkyfile USING (diff_id)
        WHERE rawExp.exp_id = $expId AND camRun.magicked
SQL

    $query->execute;
    return $query->fetchall_arrayref();
}

###########################################################################
#
# Returns camera-stage smf file for this exposure as used in the provided DVO Db
#
###########################################################################
sub getCameraStageSmfForThisDvoDb {
    my ($self, $dvoDb, $expId) = @_;

    my $query = $self->{_db}->prepare(<<SQL);
    SELECT path_base 
        FROM camProcessedExp 
        JOIN addRun USING(cam_id)   
        JOIN camRun USING(cam_id)  
        JOIN chipRun USING(chip_id)   
        JOIN rawExp USING(exp_id)   
        WHERE addRun.dvodb LIKE '$dvoDb' 
        AND exp_id = $expId;
SQL

    $query->execute;
    return $query->fetchrow_array();
}

###########################################################################
#
# Returns latest processed camera-stage smf file for this exposure ID
#
###########################################################################
sub getCameraStageSmf {
    my ($self, $expId) = @_;

    # DESC and LIMIT 1 is to make sure we get most recently processed
    my $query = $self->{_db}->prepare(<<SQL);

    SELECT 
        camProcessedExp.path_base
        FROM warpRun
        JOIN fakeRun USING(fake_id)
        JOIN camRun USING(cam_id)
        JOIN camProcessedExp USING(cam_id)
        JOIN chipRun USING(chip_id)
        JOIN rawExp USING (exp_id)
        WHERE rawExp.exp_id = $expId AND camRun.magicked
        ORDER BY camRun.cam_id DESC LIMIT 1
SQL

    $query->execute;
    return $query->fetchrow_array();
}

###########################################################################
#
# Returns a list of exposure IDs in a given DVO database  
#
###########################################################################
sub getExposureListFromDvoDb {
    my ($self, $dvoDb, $exposures, $lastExposure) = @_;

    if (!defined $lastExposure) {$lastExposure = 0;}

    my $query = $self->{_db}->prepare(<<SQL);
    SELECT exp_id, exp_name, camRun.dist_group 
        FROM addRun 
        JOIN camRun USING(cam_id) 
        JOIN chipRun USING(chip_id) 
        JOIN rawExp USING(exp_id) 
        WHERE addRun.dvodb LIKE '$dvoDb' 
        AND addRun.state = 'full'
        AND rawExp.exp_id > $lastExposure
        ORDER BY exp_id ASC;
SQL

    #AND rawExp.decl >= '-0.157079633' AND rawExp.decl <= '0.157079633' Jims Dec range

    $query->execute;
    ${$exposures} = $query->fetchall_arrayref();
    my $numOfExposures = scalar @{${$exposures}};

    # message
    if ($numOfExposures) {print "* Found $numOfExposures exposures";}
    else { print "* No exposures found"; }
    print " in DVO Db '$dvoDb' (counting from exposure $lastExposure)\n";

    return ($numOfExposures) ? 1 : 0;
}

###########################################################################
#
# Returns info for this exposure ID for given DVO Db
#
###########################################################################
sub getSingleExposureFromDvoDb {
    my ($self, $dvoDb, $expId, $exposures) = @_;

    my $query = $self->{_db}->prepare(<<SQL);
    SELECT exp_id, exp_name, camRun.dist_group 
        FROM addRun 
        JOIN camRun USING(cam_id) 
        JOIN chipRun USING(chip_id) 
        JOIN rawExp USING(exp_id) 
        WHERE addRun.dvodb LIKE '$dvoDb' 
        AND exp_id = $expId;
SQL

    $query->execute;

    ${$exposures} = $query->fetchall_arrayref();

    if (scalar @{${$exposures}} > 0) {
    
        print "* Found exposure $expId in DVO Db '$dvoDb'\n";
        return 1;
    }

    print "* Exposure $expId NOT found in DVO Db '$dvoDb'\n";
    return 0;
}

1;
