from tortoise import fields

from .base import BaseModel, TimestampMixin


class BaseCompanyModel(BaseModel, TimestampMixin):
    source_company_id = fields.CharField(max_length=128, unique=True, index=True, description="来源站点公司ID")
    company_name = fields.CharField(max_length=255, index=True, description="公司名称")
    company_type = fields.CharField(max_length=100, null=True, description="公司性质")
    industry = fields.CharField(max_length=255, null=True, description="行业")
    company_size = fields.CharField(max_length=100, null=True, description="公司规模")
    financing_stage = fields.CharField(max_length=100, null=True, description="融资阶段")
    city = fields.CharField(max_length=100, null=True, description="城市")
    address = fields.TextField(null=True, description="地址")
    website = fields.CharField(max_length=500, null=True, description="官网")
    logo_url = fields.CharField(max_length=1000, null=True, description="Logo地址")
    description = fields.TextField(null=True, description="公司简介")
    raw_json = fields.JSONField(description="原始公司JSON")
    first_crawled_at = fields.DatetimeField(index=True, description="首次抓取时间")
    last_crawled_at = fields.DatetimeField(index=True, description="最后抓取时间")

    class Meta:
        abstract = True


class BossCompany(BaseCompanyModel):
    class Meta:
        table = "boss_company"


class QcwyCompany(BaseCompanyModel):
    class Meta:
        table = "qcwy_company"


class ZhilianCompany(BaseCompanyModel):
    class Meta:
        table = "zhilian_company"


class CompanyCleaningQueue(BaseModel, TimestampMixin):
    source = fields.CharField(max_length=20, index=True, description="来源平台")
    company_id = fields.CharField(max_length=128, index=True, description="来源站点公司ID")
    company_name = fields.CharField(max_length=255, null=True, description="公司名称")
    status = fields.CharField(max_length=20, default="pending", index=True, description="状态")
    error_msg = fields.TextField(null=True, description="错误信息")
    retry_count = fields.IntField(default=0, description="重试次数")
    started_at = fields.DatetimeField(null=True, description="开始处理时间")
    finished_at = fields.DatetimeField(null=True, description="处理结束时间")
    jobs_fetched = fields.IntField(default=0, description="抓取到的职位数")
    jobs_stored = fields.IntField(default=0, description="写入ClickHouse成功数")
    jobs_duplicate = fields.IntField(default=0, description="职位重复数")
    jobs_failed = fields.IntField(default=0, description="职位写入失败数")
    jobs_error_msg = fields.TextField(null=True, description="职位同步错误信息")

    class Meta:
        table = "company_cleaning_queue"
        unique_together = (("source", "company_id"),)
