from peewee import *
from playhouse.shortcuts import model_to_dict

db = SqliteDatabase(
    'card_creation.db',
    pragmas={
        'journal_mode': 'wal',
        'cache_size': -1 * 64000,
        'synchronous': 0
    }
)


class BaseModel(Model):
    class Meta:
        database = db


class Cardset(BaseModel):
    set_title = CharField()
    set_subtitle = CharField(null=True)


class Player(BaseModel):
    sba_id = IntegerField(primary_key=True)
    name = CharField()
    fg_id = IntegerField()
    br_id = CharField()
    offense_col = IntegerField()
    hand = CharField(default='R')


db.create_tables([Cardset, Player])


class BatterRatings(BaseModel):
    id = CharField(unique=True, primary_key=True)
    player = ForeignKeyField(Player)
    cardset = ForeignKeyField(Cardset)
    vs_hand = FloatField()
    is_prep = BooleanField()
    homerun = FloatField()
    bp_homerun = FloatField()
    triple = FloatField()
    double_three = FloatField()
    double_two = FloatField()
    double_pull = FloatField()
    single_two = FloatField()
    single_one = FloatField()
    single_center = FloatField()
    bp_single = FloatField()
    hbp = FloatField()
    walk = FloatField()
    strikeout = FloatField()
    lineout = FloatField()
    popout = FloatField()
    flyout_a = FloatField()
    flyout_bq = FloatField()
    flyout_lf_b = FloatField()
    flyout_rf_b = FloatField()
    groundout_a = FloatField()
    groundout_b = FloatField()
    groundout_c = FloatField()
    avg = FloatField(null=True)
    obp = FloatField(null=True)
    slg = FloatField(null=True)


class PitcherRatings(BaseModel):
    id = CharField(unique=True, primary_key=True)
    player = ForeignKeyField(Player)
    cardset = ForeignKeyField(Cardset)
    vs_hand = CharField()
    is_prep = BooleanField()
    homerun = FloatField()
    bp_homerun = FloatField()
    triple = FloatField()
    double_three = FloatField()
    double_two = FloatField()
    double_cf = FloatField()
    single_two = FloatField()
    single_one = FloatField()
    single_center = FloatField()
    bp_single = FloatField()
    hbp = FloatField()
    walk = FloatField()
    strikeout = FloatField()
    fo_slap = FloatField()
    fo_center = FloatField()
    groundout_a = FloatField()
    groundout_b = FloatField()
    xcheck_p = FloatField()
    xcheck_c = FloatField()
    xcheck_1b = FloatField()
    xcheck_2b = FloatField()
    xcheck_3b = FloatField()
    xcheck_ss = FloatField()
    xcheck_lf = FloatField()
    xcheck_cf = FloatField()
    xcheck_rf = FloatField()
    avg = FloatField(null=True)
    obp = FloatField(null=True)
    slg = FloatField(null=True)


db.create_tables([BatterRatings, PitcherRatings])


class CardColumns(BaseModel):
    id = CharField(unique=True, primary_key=True)
    player = ForeignKeyField(Player)
    hand = CharField()
    b_ratings = ForeignKeyField(BatterRatings, null=True)
    p_ratings = ForeignKeyField(PitcherRatings, null=True)
    one_dice = CharField()
    one_results = CharField()
    one_splits = CharField()
    two_dice = CharField()
    two_results = CharField()
    two_splits = CharField()
    three_dice = CharField()
    three_results = CharField()
    three_splits = CharField()


class Position(BaseModel):
    player = ForeignKeyField(Player)
    cardset = ForeignKeyField(Cardset)
    position = CharField()
    innings = IntegerField()
    range = IntegerField()
    error = IntegerField()
    arm = CharField(null=True)
    pb = IntegerField(null=True)
    overthrow = IntegerField(null=True)


class BatterData(BaseModel):
    player = ForeignKeyField(Player)
    cardset = ForeignKeyField(Cardset)
    stealing = CharField()
    st_low = IntegerField()
    st_high = IntegerField()
    st_auto = BooleanField()
    st_jump = FloatField()
    bunting = CharField(null=True)
    hit_and_run = CharField(null=True)
    running = CharField()


class PitcherData(BaseModel):
    player = ForeignKeyField(Player)
    cardset = ForeignKeyField(Cardset)
    balk = IntegerField(null=True)
    wild_pitch = IntegerField(null=True)
    hold = CharField()
    starter_rating = IntegerField()
    relief_rating = IntegerField()
    closer_rating = IntegerField(null=True)
    batting = CharField(null=True)


db.create_tables([CardColumns, Position, BatterData, PitcherData])


class CardOutput(BaseModel):
    name = CharField()
    hand = CharField()
    positions = CharField()
    stealing = CharField()
    bunting = CharField()
    hitandrun = CharField()
    running = CharField()


db.close()
