import sys
# the sqlalchemy db object
from app import db

# we have to import the models so that sqlalchemy can detect them and create the db
# how else would it know what to create ?
from app.datalogger.models import *


def main():
    # creates the database
    db.create_all()

    # after the last command you should now be able to see database.db file
    # in the project folder


def seed():
    # Device Type Seeds
    device_type_CLX = Device_type(device_type='CLX')
    device_type_u800 = Device_type(device_type='Micro800')
    device_type_E300 = Device_type(device_type='E300')

    db.session.add(device_type_CLX)
    db.session.add(device_type_u800)
    db.session.add(device_type_E300)

    db.session.add(Device(device_type_id=1, address="192.168.1.10"))

    ev0 = EventConfig(name='Flow Line Pressure', event_type="analog", tag='Safety_Flow_Line_Pressure', device_id=1)
    db.session.add(ev0)
    ev1 = EventConfig(name='Load HiHi', event_type="analog", tag='Safety_Load_HiHi', device_id=1)
    db.session.add(ev1)
    ev2 = EventConfig(name='Load Lo', event_type="analog", tag='Safety_Load_Lo', device_id=1)
    db.session.add(ev2)
    ev3 = EventConfig(name='Tubing Pressure', event_type="analog", tag='Safety_Tubing_Pressure', device_id=1)
    db.session.add(ev3)
    ev4 = EventConfig(name='Load Hi', event_type="analog", tag='Safety_Load_Hi', device_id=1)
    db.session.add(ev4)
    ev5 = EventConfig(name='Casing Pressure', event_type="analog", tag='Safety_Casing_Pressure', device_id=1)
    db.session.add(ev5)
    ev6 = EventConfig(name='Fluid Load', event_type="analog", tag='Safety_Fluid_Load', device_id=1)
    db.session.add(ev6)
    ev7 = EventConfig(name='Flowmeter', event_type="analog", tag='Safety_Flowmeter', device_id=1)
    db.session.add(ev7)
    ev8 = EventConfig(name='Speed', event_type="analog", tag='Safety_Speed', device_id=1)
    db.session.add(ev8)
    ev9 = EventConfig(name='Load LoLo', event_type="analog", tag='Safety_Load_LoLo', device_id=1)
    db.session.add(ev9)
    ev10 = EventConfig(name='Inclinometer', event_type="analog", tag='Safety_Inclinometer', device_id=1)
    db.session.add(ev10)
    ev11 = EventConfig(name='Unit Jogged', event_type="bit", tag='Pump.Jog', condition='Unit Jog', device_id=1)
    db.session.add(ev11)
    ev12 = EventConfig(name='Restart (Auto Mode)', event_type="bit", tag='Pump.Auto_Restart', condition='Unit Start', device_id=1)
    db.session.add(ev12)
    ev13 = EventConfig(name='Pump Off (POC Mode)', event_type="bit", tag='Pump.POC_Stop', condition='Unit Stop', device_id=1)
    db.session.add(ev13)
    ev14 = EventConfig(name='Restart (Timer Mode)', event_type="bit", tag='Pump.Timed_Restart', condition='Unit Start', device_id=1)
    db.session.add(ev14)
    ev15 = EventConfig(name='Restart (POC Mode)', event_type="bit", tag='Pump.POC_Restart', condition='Unit Start', device_id=1)
    db.session.add(ev15)
    ev16 = EventConfig(name='Pump Off (Auto Mode)', event_type="bit", tag='Pump.Auto_Stop', condition='Unit Stop', device_id=1)
    db.session.add(ev16)
    ev17 = EventConfig(name='Peak Energy Restart', event_type="bit", tag='PeakEnergy.Restart', condition='Unit Start', device_id=1)
    db.session.add(ev17)
    ev18 = EventConfig(name='Peak Energy Stop', event_type="bit", tag='PeakEnergy.Stop', condition='Unit Stop', device_id=1)
    db.session.add(ev18)
    ev19 = EventConfig(name='User Initiated Start', event_type="bit", tag='Pump.Start', condition='Unit Start', device_id=1)
    db.session.add(ev19)
    ev20 = EventConfig(name='User Initiated Stop', event_type="bit", tag='Pump.Stop', condition='Unit Stop', device_id=1)
    db.session.add(ev20)
    ev21 = EventConfig(name='Pump Off (Timer Mode)', event_type="bit", tag='Pump.Timed_Stop', condition='Unit Stop', device_id=1)
    db.session.add(ev21)

    polished_rod_hp = Tag(name='Polished Rod HP', tag_category='poc', tag='Card_Past[1].Polished_Rod_HP', device_id=1, description='Polished Rod HP', data_type='REAL', change_threshold=1.0, min_expected=0, max_expected=20, units='HP', guarantee_sec=3600)
    db.session.add(polished_rod_hp)
    downhole_max_load = Tag(name='Peak Downhole Load', tag_category='poc', tag='Card_Past[1].Downhole_Max_Load.Load', device_id=1, description='Maximum value of downhole load for the current stroke', data_type='REAL', change_threshold=400.0, min_expected=-10000, max_expected=10000, units='lbs', guarantee_sec=3600)
    db.session.add(downhole_max_load)
    downhole_gross_stroke = Tag(name='Downhole Gross Stroke', tag_category='poc', tag='Card_Past[1].Downhole_GrossStroke', device_id=1, description='Gross Stroke Length', data_type='REAL', change_threshold=2.0, min_expected=0, max_expected=150, units='in', guarantee_sec=3600)
    db.session.add(downhole_gross_stroke)
    spm = Tag(name='Stroke Speed', tag_category='poc', tag='Card_Past[1].SPM', device_id=1, description='Current Stroke Speed', data_type='REAL', change_threshold=0.5, min_expected=0, max_expected=20, units='SPM', guarantee_sec=3600)
    db.session.add(spm)
    tubing_head_pressure = Tag(name='Tubing Head Pressure', tag_category='poc', tag='Card_Past[1].Params.Tubing_Head_Pressure', device_id=1, description='Pressure at the tubing head', data_type='REAL', change_threshold=5.0, min_expected=0, max_expected=500, units='PSI', guarantee_sec=3600)
    db.session.add(tubing_head_pressure)
    surface_min_load = Tag(name='Minimum Polished Rod Load', tag_category='poc', tag='Card_Past[1].Surface_Min.Load', device_id=1, description='Minimum value of the polished rod load this stroke', data_type='REAL', change_threshold=400.0, min_expected=0, max_expected=25000, units='lbs', guarantee_sec=3600)
    db.session.add(surface_min_load)
    downhole_fluid_load = Tag(name='Fluid Load', tag_category='poc', tag='Card_Past[1].Downhole_FluidLoad', device_id=1, description='Fluid Load of downhole card', data_type='REAL', change_threshold=400.0, min_expected=0, max_expected=10000, units='lbs', guarantee_sec=3600)
    db.session.add(downhole_fluid_load)
    downhole_max_position = Tag(name='Downhole Max. Position', tag_category='poc', tag='Card_Past[1].Downhole_Max_Position.Position', device_id=1, description='Maximum Position in the downhole card', data_type='REAL', change_threshold=2.0, min_expected=0, max_expected=1500, units='in', guarantee_sec=3600)
    db.session.add(downhole_max_position)
    downhole_net_stroke = Tag(name='Downhole Net Stroke', tag_category='poc', tag='Card_Past[1].Downhole_NetStroke', device_id=1, description='Net Stroke Length', data_type='REAL', change_threshold=2.0, min_expected=0, max_expected=150, units='in', guarantee_sec=3600)
    db.session.add(downhole_net_stroke)
    fillage_percent = Tag(name='Pump Fill Percent', tag_category='poc', tag='Card_Past[1].Fillage_Percent', device_id=1, description='Fill percent of the downhole pump', data_type='REAL', change_threshold=5.0, min_expected=0, max_expected=100, units='%', guarantee_sec=3600)
    db.session.add(fillage_percent)
    pump_hp = Tag(name='Downhole Pump HP', tag_category='poc', tag='Card_Past[1].Pump_HP', device_id=1, description='Downhole Pump Horsepower', data_type='REAL', change_threshold=1.0, min_expected=0, max_expected=20, units='HP', guarantee_sec=3600)
    db.session.add(pump_hp)
    surface_min_position = Tag(name='Surface Min. Position', tag_category='poc', tag='Card_Past[1].Surface_Min.Position', device_id=1, description='Minimum position in the surface card', data_type='REAL', change_threshold=1.0, min_expected=0, max_expected=150, units='in', guarantee_sec=3600)
    db.session.add(surface_min_position)
    pump_intake_pressure = Tag(name='Pump Intake Pressure', tag_category='poc', tag='Card_Past[1].Pump_Intake_Pressure', device_id=1, description='Pressure at the intake of the pump', data_type='REAL', change_threshold=200.0, min_expected=0, max_expected=20000, units='PSI', guarantee_sec=3600)
    db.session.add(pump_intake_pressure)
    surface_max_position = Tag(name='Surface Max. Position', tag_category='poc', tag='Card_Past[1].Surface_Max.Position', device_id=1, description='Maximum position in the surface card', data_type='REAL', change_threshold=1.0, min_expected=0, max_expected=1000, units='in', guarantee_sec=3600)
    db.session.add(surface_max_position)
    tubing_movement = Tag(name='Tubing Movement', tag_category='poc', tag='Card_Past[1].Tubing_Movement', device_id=1, description='Tubing Movement in the downhole card', data_type='REAL', change_threshold=1.0, min_expected=0, max_expected=100, units='in', guarantee_sec=3600)
    db.session.add(tubing_movement)
    downhole_min_position = Tag(name='Downhole Min. Position', tag_category='poc', tag='Card_Past[1].Downhole_Min_Position.Position', device_id=1, description='Minimum Position in the downhole card', data_type='REAL', change_threshold=2.0, min_expected=0, max_expected=150, units='in', guarantee_sec=3600)
    db.session.add(downhole_min_position)
    surface_max_load = Tag(name='Peak Polished Rod Load', tag_category='poc', tag='Card_Past[1].Surface_Max.Load', device_id=1, description='Peak Polished Rod Load in the surface card', data_type='REAL', change_threshold=400.0, min_expected=5000, max_expected=25000, units='lbs', guarantee_sec=3600)
    db.session.add(surface_max_load)
    downhole_min_load = Tag(name='Minimum Downhole Load', tag_category='poc', tag='Card_Past[1].Downhole_Min_Load.Load', device_id=1, description='Minimum load in the downhole card', data_type='REAL', change_threshold=400.0, min_expected=-20000, max_expected=20000, units='lbs', guarantee_sec=3600)
    db.session.add(downhole_min_load)
    surface_stroke_length = Tag(name='Surface Stroke Length', tag_category='poc', tag='Card_Past[1].Surface_StrokeLength', device_id=1, description='Stroke length of the surface card', data_type='REAL', change_threshold=1.0, min_expected=0, max_expected=150, units='in', guarantee_sec=3600)
    db.session.add(surface_stroke_length)
    downhole_adjusted_gross_stroke = Tag(name='Downhole Adjusted Gross Stroke', tag_category='poc', tag='Card_Past[1].Downhole_AdjustedGrossStroke', device_id=1, description='Gross Stroke after adjusting for tubing movement', data_type='REAL', change_threshold=2.0, min_expected=0, max_expected=150, units='in', guarantee_sec=3600)
    db.session.add(downhole_adjusted_gross_stroke)
    fluid_level = Tag(name='Fluid Level', tag_category='poc', tag='Card_Past[1].Fluid_Above_Pump', device_id=1, description='Fluid level above the pump', data_type='REAL', change_threshold=200.0, min_expected=0, max_expected=15000, units='ft', guarantee_sec=3600)
    db.session.add(fluid_level)
    stroke_production = Tag(name='Stroke Production', tag_category='poc', tag='Stroke_Production', device_id=1, description='Amount of fluid produced in the current stroke', data_type='REAL', change_threshold=0.005, min_expected=0.00, max_expected=0.01, units='BBL', guarantee_sec=3600)
    db.session.add(stroke_production)

    num_tapers = BackupRestore(tag="Card_Current.Params.Num_Tapers", device_id=1)
    taper_length = [BackupRestore(tag="Taper.Taper[{}].Setup.Length".format(i), device_id=1) for i in range(1, 10)]
    taper_diameter = [BackupRestore(tag="Taper.Taper[{}].Setup.Diameter".format(i), device_id=1) for i in range(1, 10)]
    taper_material = [BackupRestore(tag="Taper.Taper[{}].Setup.Material".format(i), device_id=1) for i in range(1, 10)]
    pump_diameter = BackupRestore(tag='UnitConfig.Pump_Diameter', device_id=1)

    db.session.add(num_tapers)
    for i in range(0, len(taper_length)):
        db.session.add(taper_length[i])

    for i in range(0, len(taper_diameter)):
        db.session.add(taper_diameter[i])

    for i in range(0, len(taper_material)):
        db.session.add(taper_material[i])

    db.session.add(pump_diameter)

    db.session.commit()


if __name__ == '__main__':
    main()
    try:
        if sys.argv[1] == "seed":
            seed()
            print("Created database.db file and seeded data.")
    except Exception as e:
        print("Created database.db file. To seed data, run 'python setupdb.py seed'")
