"""CRM entity schema registrations.

Each main business entity registers an ``EntitySchema`` so the frontend
can render forms, tables and filters generically.  These are auto-
discovered by ``SchemaConfig.ready()`` which calls
``autodiscover_modules("schemas")``.
"""

from simorgh.apps.schema.registry import (
    ActionSpec,
    EntitySchema,
    FieldSpec,
    ViewSpec,
    register_entity,
)

register_entity(
    EntitySchema(
        name="crm.contact",
        label_key="crm.contacts.title",
        plural_label_key="crm.contacts.plural",
        list_endpoint="/api/v1/crm/contacts/",
        detail_endpoint="/api/v1/crm/contacts/{public_id}/",
        view_permission="crm.contact.view",
        create_permission="crm.contact.add",
        update_permission="crm.contact.change",
        delete_permission="crm.contact.delete",
        filterable_fields=("type", "tags", "is_customer", "is_vendor", "created_at"),
        sortable_fields=("name", "type", "created_at"),
        fields=(
            FieldSpec(name="public_id", type="uuid", label_key="common.id", read_only=True),
            FieldSpec(name="type", type="enum", label_key="crm.contacts.fields.type",
                      enum=("person", "company", "lead"), required=True),
            FieldSpec(name="name", type="string", label_key="crm.contacts.fields.name",
                      required=True, max_length=255, semantic_type="full_name", ai_hint="Full contact name"),
            FieldSpec(name="email", type="string", label_key="crm.contacts.fields.email",
                      semantic_type="email", ai_hint="Primary email address"),
            FieldSpec(name="phone", type="string", label_key="crm.contacts.fields.phone",
                      semantic_type="phone"),
            FieldSpec(name="website", type="string", label_key="crm.contacts.fields.website",
                      max_length=255, semantic_type="url"),
            FieldSpec(name="notes", type="text", label_key="crm.contacts.fields.notes"),
            FieldSpec(name="tags", type="m2m", label_key="crm.contacts.fields.tags",
                      fk_entity="crm.contact_tag"),
            FieldSpec(name="is_customer", type="bool", label_key="crm.contacts.fields.isCustomer",
                      default=False),
            FieldSpec(name="is_vendor", type="bool", label_key="crm.contacts.fields.isVendor",
                      default=False),
            FieldSpec(name="created_at", type="datetime", label_key="common.createdAt",
                      read_only=True),
        ),
        views=(
            ViewSpec(kind="list", fields=("name", "type", "email", "phone", "is_customer", "created_at")),
            ViewSpec(kind="detail", fields=("public_id", "type", "name", "email", "phone",
                                            "website", "notes", "tags", "is_customer",
                                            "is_vendor", "created_at")),
            ViewSpec(kind="form", fields=("type", "name", "email", "phone", "website",
                                          "notes", "tags", "is_customer", "is_vendor")),
        ),
        actions=(
            ActionSpec(name="merge", label_key="crm.contacts.actions.merge",
                       endpoint="/api/v1/crm/contacts/{public_id}/merge/",
                       method="POST", permission="crm.contact.merge",
                       confirm_key="crm.contacts.actions.mergeConfirm"),
            ActionSpec(name="export", label_key="crm.contacts.actions.export",
                       endpoint="/api/v1/crm/contacts/export/",
                       method="POST", permission="crm.contact.export",
                       confirm_key="crm.contacts.actions.exportConfirm"),
        ),
    )
)

register_entity(
    EntitySchema(
        name="crm.contact_tag",
        label_key="crm.tags.title",
        plural_label_key="crm.tags.plural",
        list_endpoint="/api/v1/crm/tags/",
        detail_endpoint="/api/v1/crm/tags/{public_id}/",
        view_permission="crm.tag.view",
        create_permission="crm.tag.manage",
        update_permission="crm.tag.manage",
        delete_permission="crm.tag.manage",
        filterable_fields=("name", "color"),
        sortable_fields=("name",),
        fields=(
            FieldSpec(name="public_id", type="uuid", label_key="common.id", read_only=True),
            FieldSpec(name="name", type="string", label_key="crm.tags.fields.name",
                      required=True, max_length=64),
            FieldSpec(name="color", type="string", label_key="crm.tags.fields.color",
                      max_length=16, default=""),
        ),
        views=(
            ViewSpec(kind="list", fields=("name", "color")),
            ViewSpec(kind="form", fields=("name", "color")),
            ViewSpec(kind="detail", fields=("public_id", "name", "color")),
        ),
    )
)

register_entity(
    EntitySchema(
        name="crm.lead",
        label_key="crm.leads.title",
        plural_label_key="crm.leads.plural",
        list_endpoint="/api/v1/crm/leads/",
        detail_endpoint="/api/v1/crm/leads/{public_id}/",
        view_permission="crm.lead.view",
        create_permission="crm.lead.create",
        update_permission="crm.lead.update",
        delete_permission="crm.lead.delete",
        filterable_fields=("status", "source", "owner", "score", "created_at"),
        sortable_fields=("title", "status", "score", "created_at"),
        fields=(
            FieldSpec(name="public_id", type="uuid", label_key="common.id", read_only=True),
            FieldSpec(name="title", type="string", label_key="crm.leads.fields.title",
                      required=True, max_length=200),
            FieldSpec(name="status", type="enum", label_key="crm.leads.fields.status",
                      enum=("new", "contacted", "qualified", "converted", "lost"),
                      required=True, default="new"),
            FieldSpec(name="source", type="enum", label_key="crm.leads.fields.source",
                      enum=("website", "referral", "cold_call", "social", "event",
                            "trade_show", "manual"), default="manual"),
            FieldSpec(name="owner", type="fk", label_key="crm.leads.fields.owner",
                      fk_entity="core.user"),
            FieldSpec(name="score", type="int", label_key="crm.leads.fields.score",
                      default=0, min_value=0, max_value=100),
            FieldSpec(name="notes", type="text", label_key="crm.leads.fields.notes"),
            FieldSpec(name="created_at", type="datetime", label_key="common.createdAt",
                      read_only=True),
        ),
        views=(
            ViewSpec(kind="list", fields=("title", "status", "source", "owner", "score", "created_at")),
            ViewSpec(kind="detail", fields=("public_id", "title", "status", "source", "owner",
                                            "score", "notes", "created_at")),
            ViewSpec(kind="form", fields=("title", "status", "source", "owner", "score", "notes")),
        ),
    )
)

register_entity(
    EntitySchema(
        name="crm.opportunity",
        label_key="crm.opportunities.title",
        plural_label_key="crm.opportunities.plural",
        list_endpoint="/api/v1/crm/opportunities/",
        detail_endpoint="/api/v1/crm/opportunities/{public_id}/",
        view_permission="crm.opportunity.view",
        create_permission="crm.opportunity.create",
        update_permission="crm.opportunity.update",
        delete_permission="crm.opportunity.delete",
        filterable_fields=("status", "pipeline_stage", "owner", "amount", "expected_close", "created_at"),
        sortable_fields=("title", "amount", "probability", "expected_close", "created_at"),
        fields=(
            FieldSpec(name="public_id", type="uuid", label_key="common.id", read_only=True),
            FieldSpec(name="title", type="string", label_key="crm.opportunities.fields.title",
                      required=True, max_length=200),
            FieldSpec(name="status", type="enum", label_key="crm.opportunities.fields.status",
                      enum=("open", "won", "lost"), required=True, default="open"),
            FieldSpec(name="amount", type="decimal", label_key="crm.opportunities.fields.amount",
                      default=0, min_value=0, semantic_type="money_amount"),
            FieldSpec(name="currency", type="string", label_key="crm.opportunities.fields.currency",
                      default="USD", max_length=3),
            FieldSpec(name="pipeline_stage", type="fk", label_key="crm.opportunities.fields.pipelineStage",
                      fk_entity="crm.pipeline_stage"),
            FieldSpec(name="probability", type="int", label_key="crm.opportunities.fields.probability",
                      min_value=0, max_value=100, ai_hint="Win probability percentage"),
            FieldSpec(name="expected_close", type="date", label_key="crm.opportunities.fields.closeDate",
                      semantic_type="date"),
            FieldSpec(name="owner", type="fk", label_key="crm.opportunities.fields.owner",
                      fk_entity="core.user"),
            FieldSpec(name="contact", type="fk", label_key="crm.opportunities.fields.contact",
                      fk_entity="crm.contact"),
            FieldSpec(name="notes", type="text", label_key="crm.opportunities.fields.notes"),
            FieldSpec(name="created_at", type="datetime", label_key="common.createdAt",
                      read_only=True),
        ),
        views=(
            ViewSpec(kind="list", fields=("title", "status", "amount", "probability",
                                          "expected_close", "owner", "created_at")),
            ViewSpec(kind="detail", fields=("public_id", "title", "status", "amount", "currency",
                                            "probability", "expected_close", "owner", "contact",
                                            "notes", "created_at")),
            ViewSpec(kind="form", fields=("title", "status", "amount", "currency", "probability",
                                          "expected_close", "owner", "contact", "notes")),
        ),
        actions=(
            ActionSpec(name="export", label_key="crm.opportunities.actions.export",
                       endpoint="/api/v1/crm/opportunities/export/",
                       method="POST", permission="crm.opportunity.export"),
        ),
    )
)

register_entity(
    EntitySchema(
        name="crm.pipeline",
        label_key="crm.pipelines.title",
        plural_label_key="crm.pipelines.plural",
        list_endpoint="/api/v1/crm/pipelines/",
        detail_endpoint="/api/v1/crm/pipelines/{public_id}/",
        view_permission="crm.pipeline.view",
        create_permission="crm.pipeline.manage",
        update_permission="crm.pipeline.manage",
        delete_permission="crm.pipeline.manage",
        filterable_fields=("name", "is_default"),
        sortable_fields=("name",),
        fields=(
            FieldSpec(name="public_id", type="uuid", label_key="common.id", read_only=True),
            FieldSpec(name="name", type="string", label_key="crm.pipelines.fields.name",
                      required=True, max_length=100),
            FieldSpec(name="is_default", type="bool", label_key="crm.pipelines.fields.isDefault",
                      default=False),
            FieldSpec(name="created_at", type="datetime", label_key="common.createdAt",
                      read_only=True),
        ),
        views=(
            ViewSpec(kind="list", fields=("name", "is_default", "created_at")),
            ViewSpec(kind="form", fields=("name", "is_default")),
            ViewSpec(kind="detail", fields=("public_id", "name", "is_default", "created_at")),
        ),
    )
)

register_entity(
    EntitySchema(
        name="crm.activity",
        label_key="crm.activities.title",
        plural_label_key="crm.activities.plural",
        list_endpoint="/api/v1/crm/activities/",
        detail_endpoint="/api/v1/crm/activities/{public_id}/",
        view_permission="crm.activity.view",
        create_permission="crm.add_activity",
        update_permission="crm.change_activity",
        delete_permission="crm.delete_activity",
        filterable_fields=("type", "owner", "is_done", "due_at", "created_at"),
        sortable_fields=("title", "type", "due_at", "created_at"),
        fields=(
            FieldSpec(name="public_id", type="uuid", label_key="common.id", read_only=True),
            FieldSpec(name="type", type="enum", label_key="crm.activities.fields.type",
                      enum=("call", "meeting", "email", "task", "note"), required=True),
            FieldSpec(name="title", type="string", label_key="crm.activities.fields.title",
                      required=True, max_length=200),
            FieldSpec(name="notes", type="text", label_key="crm.activities.fields.notes"),
            FieldSpec(name="owner", type="fk", label_key="crm.activities.fields.owner",
                      fk_entity="core.user"),
            FieldSpec(name="due_at", type="datetime", label_key="crm.activities.fields.dueAt",
                      semantic_type="datetime"),
            FieldSpec(name="is_done", type="bool", label_key="crm.activities.fields.isDone",
                      default=False),
            FieldSpec(name="created_at", type="datetime", label_key="common.createdAt",
                      read_only=True),
        ),
        views=(
            ViewSpec(kind="list", fields=("title", "type", "owner", "due_at", "is_done", "created_at")),
            ViewSpec(kind="form", fields=("type", "title", "notes", "owner", "due_at", "is_done")),
            ViewSpec(kind="detail", fields=("public_id", "type", "title", "notes", "owner",
                                            "due_at", "is_done", "created_at")),
        ),
    )
)
