package database

import (
	"time"

	"github.com/google/uuid"
)

// University represents a German university/Hochschule
type University struct {
	ID               uuid.UUID `json:"id"`
	Name             string    `json:"name"`
	ShortName        *string   `json:"short_name,omitempty"`
	URL              string    `json:"url"`
	State            *string   `json:"state,omitempty"`
	UniType          *string   `json:"uni_type,omitempty"`
	StaffPagePattern *string   `json:"staff_page_pattern,omitempty"`
	CreatedAt        time.Time `json:"created_at"`
	UpdatedAt        time.Time `json:"updated_at"`
}

// Department represents a faculty/department at a university
type Department struct {
	ID           uuid.UUID  `json:"id"`
	UniversityID uuid.UUID  `json:"university_id"`
	Name         string     `json:"name"`
	NameEN       *string    `json:"name_en,omitempty"`
	URL          *string    `json:"url,omitempty"`
	Category     *string    `json:"category,omitempty"`
	ParentID     *uuid.UUID `json:"parent_id,omitempty"`
	CreatedAt    time.Time  `json:"created_at"`
	UpdatedAt    time.Time  `json:"updated_at"`
}

// UniversityStaff represents a staff member at a university
type UniversityStaff struct {
	ID              uuid.UUID  `json:"id"`
	UniversityID    uuid.UUID  `json:"university_id"`
	DepartmentID    *uuid.UUID `json:"department_id,omitempty"`
	FirstName       *string    `json:"first_name,omitempty"`
	LastName        string     `json:"last_name"`
	FullName        *string    `json:"full_name,omitempty"`
	Title           *string    `json:"title,omitempty"`
	AcademicTitle   *string    `json:"academic_title,omitempty"`
	Position        *string    `json:"position,omitempty"`
	PositionType    *string    `json:"position_type,omitempty"`
	IsProfessor     bool       `json:"is_professor"`
	Email           *string    `json:"email,omitempty"`
	Phone           *string    `json:"phone,omitempty"`
	Office          *string    `json:"office,omitempty"`
	ProfileURL      *string    `json:"profile_url,omitempty"`
	PhotoURL        *string    `json:"photo_url,omitempty"`
	ORCID           *string    `json:"orcid,omitempty"`
	GoogleScholarID *string    `json:"google_scholar_id,omitempty"`
	ResearchgateURL *string    `json:"researchgate_url,omitempty"`
	LinkedInURL     *string    `json:"linkedin_url,omitempty"`
	PersonalWebsite *string    `json:"personal_website,omitempty"`
	ResearchInterests []string `json:"research_interests,omitempty"`
	ResearchSummary *string    `json:"research_summary,omitempty"`
	SupervisorID    *uuid.UUID `json:"supervisor_id,omitempty"`
	TeamRole        *string    `json:"team_role,omitempty"` // leitung, mitarbeiter, sekretariat, hiwi, doktorand
	CrawledAt       time.Time  `json:"crawled_at"`
	LastVerified    *time.Time `json:"last_verified,omitempty"`
	IsActive        bool       `json:"is_active"`
	SourceURL       *string    `json:"source_url,omitempty"`
	CreatedAt       time.Time  `json:"created_at"`
	UpdatedAt       time.Time  `json:"updated_at"`

	// Joined fields (from views)
	UniversityName   *string `json:"university_name,omitempty"`
	UniversityShort  *string `json:"university_short,omitempty"`
	DepartmentName   *string `json:"department_name,omitempty"`
	PublicationCount int     `json:"publication_count,omitempty"`
	SupervisorName   *string `json:"supervisor_name,omitempty"`
}

// Publication represents an academic publication
type Publication struct {
	ID            uuid.UUID  `json:"id"`
	Title         string     `json:"title"`
	TitleEN       *string    `json:"title_en,omitempty"`
	Abstract      *string    `json:"abstract,omitempty"`
	AbstractEN    *string    `json:"abstract_en,omitempty"`
	Year          *int       `json:"year,omitempty"`
	Month         *int       `json:"month,omitempty"`
	PubType       *string    `json:"pub_type,omitempty"`
	Venue         *string    `json:"venue,omitempty"`
	VenueShort    *string    `json:"venue_short,omitempty"`
	Publisher     *string    `json:"publisher,omitempty"`
	DOI           *string    `json:"doi,omitempty"`
	ISBN          *string    `json:"isbn,omitempty"`
	ISSN          *string    `json:"issn,omitempty"`
	ArxivID       *string    `json:"arxiv_id,omitempty"`
	PubmedID      *string    `json:"pubmed_id,omitempty"`
	URL           *string    `json:"url,omitempty"`
	PDFURL        *string    `json:"pdf_url,omitempty"`
	CitationCount int        `json:"citation_count"`
	Keywords      []string   `json:"keywords,omitempty"`
	Topics        []string   `json:"topics,omitempty"`
	Source        *string    `json:"source,omitempty"`
	RawData       []byte     `json:"raw_data,omitempty"`
	CrawledAt     time.Time  `json:"crawled_at"`
	CreatedAt     time.Time  `json:"created_at"`
	UpdatedAt     time.Time  `json:"updated_at"`

	// Joined fields
	Authors      []string `json:"authors,omitempty"`
	AuthorCount  int      `json:"author_count,omitempty"`
}

// StaffPublication represents the N:M relationship between staff and publications
type StaffPublication struct {
	StaffID         uuid.UUID `json:"staff_id"`
	PublicationID   uuid.UUID `json:"publication_id"`
	AuthorPosition  *int      `json:"author_position,omitempty"`
	IsCorresponding bool      `json:"is_corresponding"`
	CreatedAt       time.Time `json:"created_at"`
}

// UniversityCrawlStatus tracks crawl progress for a university
type UniversityCrawlStatus struct {
	UniversityID       uuid.UUID  `json:"university_id"`
	LastStaffCrawl     *time.Time `json:"last_staff_crawl,omitempty"`
	StaffCrawlStatus   string     `json:"staff_crawl_status"`
	StaffCount         int        `json:"staff_count"`
	StaffErrors        []string   `json:"staff_errors,omitempty"`
	LastPubCrawl       *time.Time `json:"last_pub_crawl,omitempty"`
	PubCrawlStatus     string     `json:"pub_crawl_status"`
	PubCount           int        `json:"pub_count"`
	PubErrors          []string   `json:"pub_errors,omitempty"`
	NextScheduledCrawl *time.Time `json:"next_scheduled_crawl,omitempty"`
	CrawlPriority      int        `json:"crawl_priority"`
	CreatedAt          time.Time  `json:"created_at"`
	UpdatedAt          time.Time  `json:"updated_at"`
}

// CrawlHistory represents a crawl audit log entry
type CrawlHistory struct {
	ID           uuid.UUID  `json:"id"`
	UniversityID *uuid.UUID `json:"university_id,omitempty"`
	CrawlType    string     `json:"crawl_type"`
	Status       string     `json:"status"`
	StartedAt    time.Time  `json:"started_at"`
	CompletedAt  *time.Time `json:"completed_at,omitempty"`
	ItemsFound   int        `json:"items_found"`
	ItemsNew     int        `json:"items_new"`
	ItemsUpdated int        `json:"items_updated"`
	Errors       []byte     `json:"errors,omitempty"`
	Metadata     []byte     `json:"metadata,omitempty"`
}

// StaffSearchParams contains parameters for searching staff
type StaffSearchParams struct {
	Query        string     `json:"query,omitempty"`
	UniversityID *uuid.UUID `json:"university_id,omitempty"`
	DepartmentID *uuid.UUID `json:"department_id,omitempty"`
	State        *string    `json:"state,omitempty"`
	UniType      *string    `json:"uni_type,omitempty"`
	PositionType *string    `json:"position_type,omitempty"`
	IsProfessor  *bool      `json:"is_professor,omitempty"`
	Limit        int        `json:"limit,omitempty"`
	Offset       int        `json:"offset,omitempty"`
}

// StaffSearchResult contains search results for staff
type StaffSearchResult struct {
	Staff      []UniversityStaff `json:"staff"`
	Total      int               `json:"total"`
	Limit      int               `json:"limit"`
	Offset     int               `json:"offset"`
	Query      string            `json:"query,omitempty"`
}

// PublicationSearchParams contains parameters for searching publications
type PublicationSearchParams struct {
	Query     string     `json:"query,omitempty"`
	StaffID   *uuid.UUID `json:"staff_id,omitempty"`
	Year      *int       `json:"year,omitempty"`
	YearFrom  *int       `json:"year_from,omitempty"`
	YearTo    *int       `json:"year_to,omitempty"`
	PubType   *string    `json:"pub_type,omitempty"`
	Limit     int        `json:"limit,omitempty"`
	Offset    int        `json:"offset,omitempty"`
}

// PublicationSearchResult contains search results for publications
type PublicationSearchResult struct {
	Publications []Publication `json:"publications"`
	Total        int           `json:"total"`
	Limit        int           `json:"limit"`
	Offset       int           `json:"offset"`
	Query        string        `json:"query,omitempty"`
}

// StaffStats contains statistics about staff data
type StaffStats struct {
	TotalStaff        int            `json:"total_staff"`
	TotalProfessors   int            `json:"total_professors"`
	TotalPublications int            `json:"total_publications"`
	TotalUniversities int            `json:"total_universities"`
	ByState           map[string]int `json:"by_state,omitempty"`
	ByUniType         map[string]int `json:"by_uni_type,omitempty"`
	ByPositionType    map[string]int `json:"by_position_type,omitempty"`
	RecentCrawls      []CrawlHistory `json:"recent_crawls,omitempty"`
}
