export interface Tenant {
  id: string;
  name: string;
  slug: string;
  settings: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

export interface TenantWithRole extends Tenant {
  role: string;
}

export interface UserTenant {
  user_id: string;
  tenant_id: string;
  role: string;
  email: string;
  created_at: string;
}

export interface Case {
  id: string;
  tenant_id: string;
  case_number: string;
  title: string;
  case_type?: string;
  court?: string;
  court_ref?: string;
  status: string;
  ai_summary?: string;
  metadata: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

export interface Party {
  id: string;
  tenant_id: string;
  case_id: string;
  name: string;
  role?: string;
  representative?: string;
  contact_info: Record<string, unknown>;
}

export interface Deadline {
  id: string;
  tenant_id: string;
  case_id: string;
  title: string;
  description?: string;
  due_date: string;
  original_due_date?: string;
  warning_date?: string;
  source: string;
  rule_id?: string;
  status: string;
  completed_at?: string;
  notes?: string;
  created_at: string;
  updated_at: string;
}

export interface Appointment {
  id: string;
  tenant_id: string;
  case_id?: string;
  title: string;
  description?: string;
  start_at: string;
  end_at?: string;
  location?: string;
  appointment_type?: string;
  created_at: string;
  updated_at: string;
}

export interface CaseEvent {
  id: string;
  tenant_id: string;
  case_id: string;
  event_type?: string;
  title: string;
  description?: string;
  event_date?: string;
  created_by?: string;
  metadata: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

export interface Document {
  id: string;
  tenant_id: string;
  case_id: string;
  title: string;
  doc_type?: string;
  file_path?: string;
  file_size?: number;
  mime_type?: string;
  ai_extracted?: Record<string, unknown>;
  uploaded_by?: string;
  created_at: string;
  updated_at: string;
}

export interface DeadlineRule {
  id: string;
  proceeding_type_id?: number;
  parent_id?: string;
  code?: string;
  name: string;
  description?: string;
  primary_party?: string;
  event_type?: string;
  is_mandatory: boolean;
  duration_value: number;
  duration_unit: string;
  timing?: string;
  rule_code?: string;
  deadline_notes?: string;
  sequence_order: number;
  condition_rule_id?: string;
  alt_duration_value?: number;
  alt_duration_unit?: string;
  alt_rule_code?: string;
  is_spawn?: boolean;
  spawn_label?: string;
}

export interface RuleTreeNode extends DeadlineRule {
  children?: RuleTreeNode[];
}

// Timeline determination types

export interface TimelineEvent {
  id: string;
  code?: string;
  name: string;
  description?: string;
  primary_party?: string;
  event_type?: string;
  is_mandatory: boolean;
  duration_value: number;
  duration_unit: string;
  rule_code?: string;
  deadline_notes?: string;
  is_spawn: boolean;
  spawn_label?: string;
  has_condition: boolean;
  condition_rule_id?: string;
  alt_rule_code?: string;
  alt_duration_value?: number;
  alt_duration_unit?: string;
  date?: string;
  original_date?: string;
  was_adjusted: boolean;
  children?: TimelineEvent[];
}

export interface TimelineResponse {
  proceeding_type: ProceedingType;
  timeline: TimelineEvent[];
}

export interface DetermineRequest {
  proceeding_type: string;
  trigger_event_date: string;
  conditions: Record<string, boolean>;
}

export interface DetermineResponse {
  proceeding_type: string;
  proceeding_name: string;
  proceeding_color: string;
  trigger_event_date: string;
  timeline: TimelineEvent[];
  total_deadlines: number;
}

export interface BatchCreateRequest {
  deadlines: {
    title: string;
    due_date: string;
    original_due_date?: string;
    rule_id?: string;
    rule_code?: string;
    notes?: string;
  }[];
}

export interface ProceedingType {
  id: number;
  code: string;
  name: string;
  description?: string;
  jurisdiction?: string;
  category?: string;
  default_color: string;
  sort_order: number;
  is_active: boolean;
}

export interface CalculatedDeadline {
  rule_code: string;
  rule_id: string;
  title: string;
  due_date: string;
  original_due_date: string;
  was_adjusted: boolean;
}

export interface CalculateResponse {
  proceeding_type: string;
  trigger_event_date: string;
  deadlines: CalculatedDeadline[];
}

export interface CalDAVConfig {
  url: string;
  username: string;
  password: string;
  calendar_path: string;
  sync_enabled: boolean;
  sync_interval_minutes: number;
}

export interface CalDAVSyncStatus {
  tenant_id: string;
  last_sync_at: string;
  items_pushed: number;
  items_pulled: number;
  errors?: string[];
  sync_duration: string;
}

export interface CalDAVSyncResponse {
  status: string;
  sync: CalDAVSyncStatus;
  last_sync_at?: null;
}

export interface Note {
  id: string;
  tenant_id: string;
  case_id?: string;
  deadline_id?: string;
  appointment_id?: string;
  case_event_id?: string;
  content: string;
  created_by?: string;
  created_at: string;
  updated_at: string;
}

export interface CaseAssignment {
  id: string;
  case_id: string;
  user_id: string;
  role: string;
  assigned_at: string;
}

export interface UserInfo {
  user_id: string;
  tenant_id: string;
  role: UserRole;
  permissions: string[];
  is_demo: boolean;
}

export type UserRole = "owner" | "partner" | "associate" | "paralegal" | "secretary";

export const ROLE_LABELS: Record<UserRole, string> = {
  owner: "Inhaber",
  partner: "Partner",
  associate: "Anwalt",
  paralegal: "Paralegal",
  secretary: "Sekretariat",
};

export const CASE_ASSIGNMENT_ROLES = ["lead", "team", "viewer"] as const;
export type CaseAssignmentRole = (typeof CASE_ASSIGNMENT_ROLES)[number];

export const CASE_ASSIGNMENT_ROLE_LABELS: Record<CaseAssignmentRole, string> = {
  lead: "Federführend",
  team: "Team",
  viewer: "Einsicht",
};

export interface ApiError {
  error: string;
  status: number;
}

export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page: number;
  per_page: number;
}

// Dashboard types

export interface DeadlineSummary {
  overdue_count: number;
  due_this_week: number;
  due_next_week: number;
  ok_count: number;
}

export interface CaseSummary {
  active_count: number;
  new_this_month: number;
  closed_count: number;
}

export interface UpcomingDeadline {
  id: string;
  title: string;
  due_date: string;
  case_id: string;
  case_number: string;
  case_title: string;
}

export interface UpcomingAppointment {
  id: string;
  title: string;
  start_at: string;
  end_at?: string;
  location?: string;
  case_id?: string;
  case_number?: string;
  case_title?: string;
}

export interface RecentActivity {
  id: string;
  event_type?: string;
  title: string;
  case_id: string;
  case_number: string;
  event_date?: string;
  created_at: string;
}

export interface DashboardData {
  deadline_summary: DeadlineSummary;
  case_summary: CaseSummary;
  upcoming_deadlines: UpcomingDeadline[];
  upcoming_appointments: UpcomingAppointment[];
  recent_activity?: RecentActivity[];
}

// AI Extraction types

export interface ExtractedDeadline {
  title: string;
  due_date: string | null;
  duration_value?: number;
  duration_unit?: string;
  rule_reference: string;
  source_quote: string;
  confidence: number;
}

export interface ExtractionResponse {
  deadlines: ExtractedDeadline[];
  count: number;
}

// AI Document Drafting

export interface DocumentDraft {
  title: string;
  content: string;
  language: string;
}

export interface DraftDocumentRequest {
  case_id: string;
  template_type: string;
  instructions: string;
  language: string;
}

export const TEMPLATE_TYPES: Record<string, string> = {
  klageschrift: "Klageschrift",
  klageerwiderung: "Klageerwiderung",
  abmahnung: "Abmahnung",
  schriftsatz: "Schriftsatz",
  berufung: "Berufungsschrift",
  antrag: "Antrag",
  stellungnahme: "Stellungnahme",
  gutachten: "Gutachten",
  vertrag: "Vertrag",
  vollmacht: "Vollmacht",
  upc_claim: "UPC Statement of Claim",
  upc_defence: "UPC Statement of Defence",
  upc_counterclaim: "UPC Counterclaim for Revocation",
  upc_injunction: "UPC Provisional Measures",
};

export const TEMPLATE_CATEGORY_LABELS: Record<string, string> = {
  schriftsatz: "Schriftsatz",
  vertrag: "Vertrag",
  korrespondenz: "Korrespondenz",
  intern: "Intern",
};

// AI Case Strategy

export interface StrategyStep {
  priority: "high" | "medium" | "low";
  action: string;
  reasoning: string;
  deadline?: string;
}

export interface RiskItem {
  level: "high" | "medium" | "low";
  risk: string;
  mitigation: string;
}

export interface TimelineItem {
  date: string;
  event: string;
  importance: "critical" | "important" | "routine";
}

export interface StrategyRecommendation {
  summary: string;
  next_steps: StrategyStep[];
  risk_assessment: RiskItem[];
  timeline: TimelineItem[];
}

// AI Similar Case Finder

export interface SimilarCase {
  case_number: string;
  title: string;
  court: string;
  date: string;
  relevance: number;
  explanation: string;
  key_holdings: string;
  url?: string;
}

export interface SimilarCasesResponse {
  cases: SimilarCase[];
  count: number;
}

// Time Tracking

export interface TimeEntry {
  id: string;
  tenant_id: string;
  case_id: string;
  user_id: string;
  description: string;
  duration_minutes: number;
  hourly_rate: number;
  billable: boolean;
  billed?: boolean;
  activity?: string;
  date: string;
  created_at: string;
  updated_at: string;
}

// Billing

export interface InvoiceItem {
  description: string;
  amount: number;
  duration_minutes?: number;
  hourly_rate?: number;
}

export interface Invoice {
  id: string;
  tenant_id: string;
  case_id: string;
  invoice_number: string;
  client_name: string;
  client_address?: string;
  items: InvoiceItem[];
  subtotal: number;
  tax_rate: number;
  tax_amount: number;
  total: number;
  status: string;
  notes?: string;
  issued_at?: string;
  due_at?: string;
  paid_at?: string;
  created_at: string;
  updated_at: string;
}

export interface BillingRate {
  id: string;
  tenant_id: string;
  user_id?: string;
  rate: number;
  currency: string;
  valid_from: string;
  valid_to?: string;
  created_at: string;
  updated_at: string;
}

// Reports

export interface BillingReportMonthly {
  period: string;
  cases_new: number;
  cases_closed: number;
  cases_active: number;
}

export interface BillingReportByType {
  case_type: string;
  total: number;
  active: number;
  closed: number;
}

export interface BillingReport {
  total_revenue: number;
  outstanding: number;
  billable_hours: number;
  non_billable_hours: number;
  monthly: BillingReportMonthly[];
  by_type: BillingReportByType[];
}

export interface CaseReportTotal {
  opened: number;
  closed: number;
  active: number;
}

export interface CaseReportMonthly {
  period: string;
  opened: number;
  closed: number;
  active: number;
}

export interface CaseReportByType {
  case_type: string;
  count: number;
  active: number;
  closed: number;
  total: number;
}

export interface CaseReportByCourt {
  court: string;
  count: number;
}

export interface CaseReport {
  opened: number;
  closed: number;
  active: number;
  total: CaseReportTotal;
  monthly: CaseReportMonthly[];
  by_type: CaseReportByType[];
  by_court: CaseReportByCourt[];
}

export interface DeadlineReportTotal {
  total: number;
  met: number;
  missed: number;
  compliance_rate: number;
}

export interface DeadlineReportMonthly {
  period: string;
  total: number;
  met: number;
  missed: number;
  pending: number;
  compliance_rate: number;
}

export interface MissedDeadline {
  id: string;
  title: string;
  case_id: string;
  case_number: string;
  case_title: string;
  due_date: string;
  days_overdue: number;
}

export interface DeadlineReport {
  compliance_rate: number;
  met: number;
  total: DeadlineReportTotal;
  monthly: DeadlineReportMonthly[];
  missed: MissedDeadline[];
  by_case: Record<string, number>;
}

export interface WorkloadUser {
  name: string;
  user_id: string;
  hours: number;
  utilization: number;
  active_cases: number;
  deadlines: number;
  overdue: number;
  completed: number;
}

export interface WorkloadReport {
  users: WorkloadUser[];
}

// Document Templates

export interface DocumentTemplate {
  id: string;
  tenant_id: string;
  name: string;
  description?: string;
  category: string;
  content: string;
  variables: string[];
  is_system: boolean;
  created_at: string;
  updated_at: string;
}

export interface RenderResponse {
  rendered_content: string;
  content: string;
  name: string;
}

// Notifications

export interface Notification {
  id: string;
  tenant_id: string;
  type: string;
  entity_type: string;
  entity_id: string;
  title: string;
  body: string;
  sent_at?: string;
  read_at?: string;
  created_at: string;
  updated_at: string;
}

export interface NotificationPreferences {
  deadline_reminder_days: number[];
  email_enabled: boolean;
  daily_digest: boolean;
}

export interface NotificationListResponse {
  notifications: Notification[];
  data: Notification[];
  total: number;
  unread_count: number;
}

// Audit Log

export interface AuditLogEntry {
  id: string;
  tenant_id: string;
  user_id: string;
  action: string;
  entity_type: string;
  entity_id: string;
  old_values?: Record<string, unknown>;
  new_values?: Record<string, unknown>;
  ip_address?: string;
  created_at: string;
}

export interface AuditLogResponse {
  entries: AuditLogEntry[];
  total: number;
}
