#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
Authors : Jean-Damien Généro
Affiliation : French National Center for Scientific Research (CNRS)
Assigned at the Centre de recherches historiques (CRH, UMR 8558)
Date : 2022-10-11
Update :
"""


import re
import typing as t

import peewee
from flask import url_for
from playhouse.sqlite_ext import FTS5Model, RowIDField, SearchField

from app.app import db


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

class Institution(BaseModel):
	id_institution = peewee.AutoField()
	full_label = peewee.TextField()
	inst_label = peewee.TextField()
	art_inst = peewee.TextField()
	inst_place = peewee.TextField()
	inst_rank = peewee.TextField()
	inst_type = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Institution'


class State(BaseModel):
	id_state = peewee.AutoField()
	state_label = peewee.TextField()

	class Meta:
		database = db
		db_table = 'State'

class House(BaseModel):
	id_house = peewee.AutoField()
	house_label = peewee.TextField()

	class Meta:
		database = db
		db_table = 'House'

class Intervention_type(BaseModel):
	id_intev = peewee.AutoField()
	interv_label = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Intervention_type'

class Production_place(BaseModel):
	id_place = peewee.AutoField()
	placename = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Production_place'


class Diplo_type(BaseModel):
	id_diplo_type = peewee.AutoField()
	diplo_label = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Diplo_type'


class Document(BaseModel):
	id_document = peewee.AutoField()
	inst_doc = peewee.ForeignKeyField(Institution, backref='documents')
	collection_doc = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Document'

class Agent(BaseModel):
	id_agent = peewee.AutoField()
	agent_name = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Agent'


class Acte(BaseModel):
	id_acte = peewee.AutoField()
	num_acte = peewee.IntegerField()
	filename = peewee.TextField()
	date_time = peewee.TextField()  # YYYY-MM-DD
	date = peewee.TextField()  # verbose
	prod_place_acte = peewee.ForeignKeyField(Production_place, backref='actes')
	analysis = peewee.TextField()
	doc_acte = peewee.ForeignKeyField(Document, backref='actes')
	ref_acte = peewee.TextField()  # cote
	state_doc = peewee.ForeignKeyField(State, backref='actes')
	diplo_type_acte = peewee.ForeignKeyField(Diplo_type, backref='actes')

	class Meta:
		database = db
		db_table = 'Acte'

class Transcribed_by(BaseModel):
	id_transcr = peewee.AutoField()
	transcr_acte = peewee.ForeignKeyField(Acte, backref='transcribed_bys')
	transcr_agent = peewee.ForeignKeyField(Agent, backref='transcribed_bys')

	class Meta:
		database = db
		db_table = 'Transcribed_by'

class Individual(BaseModel):
	id_indiv = peewee.AutoField()
	name_indiv = peewee.TextField()
	role_indiv = peewee.TextField()
	house_indiv = peewee.ForeignKeyField(House, backref='individuals')
	date1 = peewee.TextField()
	date2 = peewee.TextField()
	date3 = peewee.TextField()

	class Meta:
		database = db
		db_table = 'Individual'

class Involved_in(BaseModel):
	id_involved_in = peewee.AutoField()
	involved_in_acte = peewee.ForeignKeyField(Acte, backref='involved_ins')
	involved_in_prince = peewee.ForeignKeyField(Individual, backref='involved_ins')
	invol_in_interv = peewee.ForeignKeyField(Intervention_type, backref='involved_ins')

	class Meta:
		database = db
		db_table = 'Involved_in'
