from django.db import models

from wagtail.models import Page
from wagtail.fields import StreamField, RichTextField
from wagtail.admin.panels import FieldPanel, MultiFieldPanel, InlinePanel
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail import blocks
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel

class SkillCategory(blocks.StructBlock):
    name = blocks.CharBlock(required=True, help_text="Category name (e.g. Frontend, Backend)")
    skills = blocks.ListBlock(
        blocks.StructBlock([
            ("name", blocks.CharBlock(required=True, help_text="Skill name (e.g. React, Django)")),
        ])
    )
    
    class Meta:
        template = 'about/blocks/skill_category.html'
        icon = 'list-ul'

class WorkExperienceBlock(blocks.StructBlock):
    role = blocks.CharBlock(required=True)
    company = blocks.CharBlock(required=True)
    period = blocks.CharBlock(required=True, help_text="e.g. 2022 - Present")
    description = blocks.RichTextBlock(required=True, features=['bold', 'italic', 'ol', 'ul', 'link'])
    
    class Meta:
        template = 'about/blocks/work_experience.html'
        icon = 'date'

class EducationBlock(blocks.StructBlock):
    degree = blocks.CharBlock(required=True)
    institution = blocks.CharBlock(required=True)
    period = blocks.CharBlock(required=True, help_text="e.g. 2011-2015")
    description = blocks.RichTextBlock(required=True, features=['bold', 'italic', 'ol', 'ul', 'link'])
    
    class Meta:
        template = 'about/blocks/education.html'
        icon = 'education'

class AboutPage(Page):
    # Basic Info
    profile_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
    bio = models.TextField(
        help_text="A brief introduction about yourself"
    )
    bio_details = RichTextField(
        blank=True,
        help_text="Additional details about your expertise and approach"
    )
    personal_info = RichTextField(
        blank=True,
        help_text="Personal interests and activities outside of work"
    )
    
    # Resume
    resume = models.ForeignKey(
        'wagtaildocs.Document',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='about_resumes',
    )
    
    # Skills and Experience
    skills = StreamField([
        ("skill_category", SkillCategory()),
    ], use_json_field=True, blank=True)
    
    work_experience = StreamField([
        ("experience", WorkExperienceBlock()),
    ], use_json_field=True, blank=True)
    
    education = StreamField([
        ("education", EducationBlock()),
    ], use_json_field=True, blank=True)

    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel('profile_image'),
            FieldPanel('bio'),
            FieldPanel('bio_details'),
            FieldPanel('personal_info'),
            FieldPanel('resume'),
        ], heading="Basic Information"),
        FieldPanel('skills', heading="Skills & Expertise"),
        FieldPanel('work_experience', heading="Work Experience"),
        FieldPanel('education', heading="Education"),
    ]
