package com.utopiaindustries.qualitychecker.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "inventory";
    // Table and columns
    public static final String PRODUCT_TABLE_NAME = "product";
    public static final String PRODUCT_COLUMN_ASIN = "asin";
    public static final String PRODUCT_COLUMN_PARENT_ASIN = "parentAsin";
    public static final String PRODUCT_COLUMN_MARKETPLACE = "marketplace";
    public static final String PRODUCT_COLUMN_SKU = "sku";
    public static final String PRODUCT_COLUMN_TITLE = "title";
    public static final String PRODUCT_COLUMN_HS_CODE = "hsCode";
    public static final String PRODUCT_COLUMN_MODEL_NUMBER = "modelNumber";
    public static final String PRODUCT_COLUMN_ITEM_PER_BOX = "itemPerBox";
    public static final String PRODUCT_COLUMN_SM_COLOR = "smColor";
    public static final String PRODUCT_COLUMN_SM_SIZE = "smSize";
    public static final String PRODUCT_COLUMN_SM_ITEM_NAME = "smItemName";
    public static final String PRODUCT_COLUMN_ITEM_PER_PACK = "itemPerPack";
    public static final String PRODUCT_COLUMN_INVENTORY = "inventory";
    public static final String PRODUCT_COLUMN_CATEGORY = "category";
    public static final String PRODUCT_COLUMN_FNSKU = "fnsku";

    //Table and Column for Inspection Label
    public static final String INSPECTION_LABEL_TABLE_NAME = "inspection_label";
    public static final String INSPECTION_LABEL_COLUMN_ID = "id";
    public static final String INSPECTION_LABEL_COLUMN_MIN_LOT_SIZE = "minLotSize";
    public static final String INSPECTION_LABEL_COLUMN_MAX_LOT_SIZE = "maxLotSize";
    public static final String INSPECTION_LABEL_COLUMN_INSPECTION_LEVEL = "inspectionLevel";
    public static final String INSPECTION_LABEL_COLUMN_SAMPLE_CODE = "sampleCode";

    //Table and Column for Quality Label
    public static final String QUALITY_LABEL_TABLE_NAME = "quality_label";
    public static final String QUALITY_LABEL_COLUMN_ID = "id";
    public static final String QUALITY_LABEL_COLUMN_SAMPLE_CODE = "sampleCode";
    public static final String QUALITY_LABEL_COLUMN_SAMPLE_SIZE = "sampleSize";
    public static final String QUALITY_LABEL_COLUMN_QUALITY_LEVEL = "qualityLevel";
    public static final String QUALITY_LABEL_COLUMN_NO_ACCEPTED_DEFECTS = "noOfAcceptedDefects";

    /*
    * dimension table
     */
    public static final String INSPECTION_DIMENSION_TABLE_NAME = "dimension";
    public static final String DIMENSION_COLUMN_ID = "id";
    public static final String DIMENSION_COLUMN_TITLE = "title";
    public static final String DIMENSION_COLUMN_CATEGORY = "category";

    /*
    *  checkpoints table
    * */
    public static final String CHECKPOINT_TABLE_NAME = "checkpoint";
    public static final String CHECKPOINT_COLUMN_ID = "id";
    public static final String CHECKPOINT_COLUMN_TITLE = "title";
    public static final String CHECKPOINT_COLUMN_CATEGORY = "category";

    /*
    *  defect table
    * */
    public static final String DEFECT_TABLE_NAME = "defect";
    public static final String DEFECT_COLUMN_DEFECT = "defect";
    public static final String DEFECT_COLUMN_CATEGORY = "category";

    /*
    *  unit table
    * */
    public static final String UNIT_TABLE_NAME = "unit";
    public static final String UNIT_COLUMN_ID = "id";
    public static final String UNIT_COLUMN_TITLE = "title";

    /*
    *  report table
    * */
    public static final String REPORT_TABLE_NAME = "report";
    public static final String REPORT_COLUMN_ID = "id";
    public static final String REPORT_COLUMN_CONTENT = "content";
    public static final String REPORT_COLUMN_SYNCED = "synced";

    /*
     *  draft report table
     * */
    public static final String DRAFT_REPORT_TABLE_NAME = "draft_report";
    public static final String DRAFT_REPORT_COLUMN_ID = "id";
    public static final String DRAFT_REPORT_COLUMN_CONTENT = "content";
    //public static final String DRAFT_REPORT_COLUMN_FNSKU = "fnsku";
    public static final String DRAFT_REPORT_COLUMN_SYNCED = "synced";


    /*
    * sku checkpoints standard
    * */
    public static final String CHECKPOINT_SKU_TABLE_NAME = "checkpoint_sku";
    public static final String CHECKPOINT_SKU_SKU = "sku";
    public static final String CHECKPOINT_SKU_MARKETPLACE = "marketplace";
    public static final String CHECKPOINT_SKU_CHECKPOINT_ID = "checkpoint_id";
    public static final String CHECKPOINT_SKU_STANDARD = "standard";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // creating product table
        String CREATE_PRODUCT_TABLE = "CREATE TABLE " + PRODUCT_TABLE_NAME + "("
                + PRODUCT_COLUMN_ASIN + " TEXT,"
                + PRODUCT_COLUMN_PARENT_ASIN + " TEXT,"
                + PRODUCT_COLUMN_MARKETPLACE + " TEXT,"
                + PRODUCT_COLUMN_SKU + " TEXT,"
                + PRODUCT_COLUMN_TITLE + " TEXT,"
                + PRODUCT_COLUMN_HS_CODE + " TEXT,"
                + PRODUCT_COLUMN_MODEL_NUMBER + " TEXT,"
                + PRODUCT_COLUMN_ITEM_PER_BOX + " INTEGER,"
                + PRODUCT_COLUMN_SM_COLOR + " TEXT,"
                + PRODUCT_COLUMN_SM_SIZE + " TEXT,"
                + PRODUCT_COLUMN_SM_ITEM_NAME + " TEXT,"
                + PRODUCT_COLUMN_ITEM_PER_PACK + " INTEGER,"
                + PRODUCT_COLUMN_INVENTORY + " INTEGER,"
                + PRODUCT_COLUMN_CATEGORY + " TEXT,"
                + PRODUCT_COLUMN_FNSKU + " TEXT,"
                + "PRIMARY KEY (" + PRODUCT_COLUMN_ASIN + ", " + PRODUCT_COLUMN_MARKETPLACE + ", " + PRODUCT_COLUMN_SKU + ")"
                + ")";


        String CREATE_INSPECTION_DIMENSION_TABLE = "CREATE TABLE " + INSPECTION_DIMENSION_TABLE_NAME + " (" +
                DIMENSION_COLUMN_ID + " INTEGER PRIMARY KEY," +
                DIMENSION_COLUMN_TITLE + " TEXT NOT NULL," +
                DIMENSION_COLUMN_CATEGORY + " TEXT NOT NULL);";

        String CREATE_CHECKPOINT_TABLE = "CREATE TABLE " + CHECKPOINT_TABLE_NAME + " (" +
                CHECKPOINT_COLUMN_ID + " INTEGER PRIMARY KEY," +
                CHECKPOINT_COLUMN_TITLE + " TEXT NOT NULL," +
                CHECKPOINT_COLUMN_CATEGORY + " TEXT NOT NULL);";

        String CREATE_DEFECT_TABLE = "CREATE TABLE " + DEFECT_TABLE_NAME + " (" +
                DEFECT_COLUMN_DEFECT + " TEXT NOT NULL," +
                DEFECT_COLUMN_CATEGORY + " TEXT NOT NULL," +
                "PRIMARY KEY (" + DEFECT_COLUMN_DEFECT + ", " + DEFECT_COLUMN_CATEGORY + ")"
                + ")";


        String CREATE_UNIT_TABLE = "CREATE TABLE " + UNIT_TABLE_NAME + " (" +
                UNIT_COLUMN_ID + " INTEGER PRIMARY KEY," +
                UNIT_COLUMN_TITLE + " TEXT NOT NULL )";

        String CREATE_REPORT_TABLE = "CREATE TABLE " + REPORT_TABLE_NAME + " (" +
                REPORT_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                REPORT_COLUMN_CONTENT + " TEXT NOT NULL," +
                REPORT_COLUMN_SYNCED + " INTEGER DEFAULT 0 CHECK(synced IN (0, 1))"
                + ")";

        String CREATE_DRAFT_REPORT_TABLE = "CREATE TABLE " + DRAFT_REPORT_TABLE_NAME + " (" +
                DRAFT_REPORT_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                DRAFT_REPORT_COLUMN_CONTENT + " TEXT NOT NULL, " +
              //  DRAFT_REPORT_COLUMN_FNSKU + " TEXT NOT NULL," +
                DRAFT_REPORT_COLUMN_SYNCED + " INTEGER DEFAULT 0 CHECK(synced IN (0, 1))"
                + ")";

        String CREATE_SKU_CHECKPOINT_TABLE = "CREATE TABLE " + CHECKPOINT_SKU_TABLE_NAME + " (" +
                CHECKPOINT_SKU_SKU + " TEXT NOT NULL," +
                CHECKPOINT_SKU_MARKETPLACE + " TEXT NOT NULL," +
                CHECKPOINT_SKU_CHECKPOINT_ID + " INTEGER NOT NULL," +
                CHECKPOINT_SKU_STANDARD + " TEXT," +
                "PRIMARY KEY (" + CHECKPOINT_SKU_SKU + ", " + CHECKPOINT_SKU_MARKETPLACE  + ", " + CHECKPOINT_SKU_CHECKPOINT_ID + "))";

        String CREATE_INSPECTION_LABEL_TABLE = "CREATE TABLE " + INSPECTION_LABEL_TABLE_NAME + " (" +
                INSPECTION_LABEL_COLUMN_ID + " INTEGER NOT NULL," +
                INSPECTION_LABEL_COLUMN_MIN_LOT_SIZE + " INTEGER NOT NULL," +
                INSPECTION_LABEL_COLUMN_MAX_LOT_SIZE + " INTEGER NOT NULL," +
                INSPECTION_LABEL_COLUMN_INSPECTION_LEVEL + " INTEGER NOT NULL," +
                INSPECTION_LABEL_COLUMN_SAMPLE_CODE + " TEXT NOT NULL )";

        String CREATE_QUALITY_LABEL_TABLE = "CREATE TABLE " + QUALITY_LABEL_TABLE_NAME + " (" +
                QUALITY_LABEL_COLUMN_ID + " INTEGER NOT NULL," +
                QUALITY_LABEL_COLUMN_SAMPLE_CODE + " TEXT NOT NULL," +
                QUALITY_LABEL_COLUMN_SAMPLE_SIZE + " INTEGER NOT NULL," +
                QUALITY_LABEL_COLUMN_QUALITY_LEVEL + " REAL NOT NULL," +
                QUALITY_LABEL_COLUMN_NO_ACCEPTED_DEFECTS + " INTEGER NOT NULL )";



        db.execSQL( CREATE_PRODUCT_TABLE );
        db.execSQL( CREATE_INSPECTION_DIMENSION_TABLE );
        db.execSQL( CREATE_CHECKPOINT_TABLE );
        db.execSQL( CREATE_DEFECT_TABLE );
        db.execSQL( CREATE_UNIT_TABLE );
        db.execSQL( CREATE_REPORT_TABLE );
        db.execSQL( CREATE_DRAFT_REPORT_TABLE);
        db.execSQL( CREATE_SKU_CHECKPOINT_TABLE );
        db.execSQL( CREATE_INSPECTION_LABEL_TABLE );
        db.execSQL( CREATE_QUALITY_LABEL_TABLE );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL( "DROP TABLE IF EXISTS " + PRODUCT_TABLE_NAME);
        db.execSQL( "DROP TABLE IF EXISTS " + INSPECTION_DIMENSION_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + CHECKPOINT_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + DEFECT_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + UNIT_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + REPORT_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + DRAFT_REPORT_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + CHECKPOINT_SKU_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + INSPECTION_LABEL_TABLE_NAME );
        db.execSQL( "DROP TABLE IF EXISTS " + QUALITY_LABEL_TABLE_NAME );
        onCreate(db);
    }
}
